﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace ET.Monitor.DAL
{
    public class StationDAL
    {
        object objJudge = null, obj = null, dlVal = null;
        string strSql = "", strErrMsg = "", plantName = "";
        DataTable dt = new DataTable();

        InvokeWebService iw = new InvokeWebService();
        string rtdbRelationAddress = "";
        string rtdbRealTimeAddress = "";
        string rtdbNameSpace = "";
        string rtdbJudge = "";

        public StationDAL()
        {
            rtdbRelationAddress = ConfigurationManager.ConnectionStrings["RelationURL"].ConnectionString;
            rtdbRealTimeAddress = ConfigurationManager.ConnectionStrings["RealTimeURL"].ConnectionString;
            rtdbNameSpace = ConfigurationManager.ConnectionStrings["NameSpace"].ConnectionString;
            rtdbJudge = ConfigurationManager.ConnectionStrings["Judge"].ConnectionString;
        }

        #region 获取电站当前故障数量
        public int GetTotalFaultDetailInfoCount(string stationId, int count, int pageIndex)
        {
            if (stationId.IndexOf("'") > -1)
                strSql = @"select count(*) from (select F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,u.T_UNITDESC_CN from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, 
                                    C.T_FAULTDESC_CN, C.T_FAULTDESC_EN From T_INFO_FAULTDETAIL A LEFT JOIN 
                                    dbo.T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=
                                    C.T_MACHINEID where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT 
                                    where T_ORGID in(" + stationId + "))) and T_ENDTIME is null) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID) a";
            else
                strSql = @"select count(*) from (select F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,u.T_UNITDESC_CN from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, 
                                    C.T_FAULTDESC_CN, C.T_FAULTDESC_EN From T_INFO_FAULTDETAIL A LEFT JOIN 
                                    dbo.T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=
                                    C.T_MACHINEID where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT 
                                    where T_ORGID ='" + stationId + "')) and T_ENDTIME is null) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID) a";

            return (int)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunRowCount", new object[] { strSql, strErrMsg });
        }
        public int GetTotalFaultDetailInfoCount(string stationId, int count, int pageIndex, string type)
        {
            strSql = @"select count(*) from (select   F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_TYPEID,F.T_PLANTDESC,u.T_UNITDESC_CN from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, pl.T_PLANTDESC,C.T_FAULTDESC_CN, C.T_FAULTDESC_EN, C.T_TYPEID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID = C.T_FAULTID AND B.T_MACHINEID = C.T_MACHINEID left join T_BASE_DISTRICT ta on B.T_DISTRICTID = ta.T_DISTRICTID left join T_INFO_PLANT pl on ta.T_ORGID = pl.T_ORGID where A.T_ORGID in (select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in (" + stationId + @"))) and A.T_ENDTIME is null and T_ENDTIME is null) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID)ft left join T_BASE_FAULTTYPE bt on ft.T_TYPEID = bt.T_TYPEID where bt.T_TYPEDESC='" + stationId + @"'";

            return (int)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunRowCount", new object[] { strSql, strErrMsg });
        }
        #endregion

        #region 获取电站当前故障信息
        public DataTable GetTotalFaultDetailInfo(string stationId, int count, int pageIndex)
        {
            if (stationId.IndexOf("'") > -1)
                strSql = @"select ft.ID_KEY, ft.T_ENDTIME,ft.T_FAULTDESC_CN,ft.T_FAULTDESC_EN,ft.T_ORGID,ft.T_STARTTIME,ft.T_UNITDESC_CN,bt.T_TYPEDESC,ft.T_PLANTDESC From (select top " + count + @" F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_TYPEID,F.T_PLANTDESC,u.T_UNITDESC_CN from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, pl.T_PLANTDESC,C.T_FAULTDESC_CN, C.T_FAULTDESC_EN, C.T_TYPEID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID = C.T_FAULTID AND B.T_MACHINEID = C.T_MACHINEID left join T_BASE_DISTRICT ta on B.T_DISTRICTID = ta.T_DISTRICTID left join T_INFO_PLANT pl on ta.T_ORGID = pl.T_ORGID where A.T_ORGID in (select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in (" + stationId + @"))) and A.T_ENDTIME is null and A.ID_KEY not in(select top " + count * (pageIndex - 1) + @" ID_KEY from T_INFO_FAULTDETAIL where T_ORGID in (select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in (" + stationId + @"))) order by ID_KEY DESC) and T_ENDTIME is null) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID ORDER BY F.ID_KEY desc)ft left join T_BASE_FAULTTYPE bt on ft.T_TYPEID = bt.T_TYPEID";
            else
                strSql = @"select ft.ID_KEY, ft.T_ENDTIME,ft.T_FAULTDESC_CN,ft.T_FAULTDESC_EN,ft.T_ORGID,ft.T_STARTTIME,ft.T_UNITDESC_CN,bt.T_TYPEDESC,ft.T_PLANTDESC From (select top " + count + @" F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_TYPEID,F.T_PLANTDESC,u.T_UNITDESC_CN from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, pl.T_PLANTDESC,C.T_FAULTDESC_CN, C.T_FAULTDESC_EN, C.T_TYPEID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID = C.T_FAULTID AND B.T_MACHINEID = C.T_MACHINEID left join T_BASE_DISTRICT ta on B.T_DISTRICTID = ta.T_DISTRICTID left join T_INFO_PLANT pl on ta.T_ORGID = pl.T_ORGID where A.T_ORGID in (select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID ='" + stationId + @"')) and A.T_ENDTIME is null and A.ID_KEY not in(select top " + count * (pageIndex - 1) + @" ID_KEY from T_INFO_FAULTDETAIL where T_ORGID in (select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID ='" + stationId + @"')) order by ID_KEY DESC) and T_ENDTIME is null) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID ORDER BY F.ID_KEY desc)ft left join T_BASE_FAULTTYPE bt on ft.T_TYPEID = bt.T_TYPEID";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }

        public DataTable GetTotalFaultDetailInfoALL(string stationId)
        {
            if (stationId.IndexOf("'") > -1)
                strSql = @"select ft.ID_KEY, ft.T_ENDTIME, ft.T_FAULTDESC_CN, ft.T_FAULTDESC_EN, ft.T_ORGID, ft.T_STARTTIME, ft.T_UNITDESC_CN, bt.T_TYPEDESC, ft.T_PLANTDESC From (select F.ID_KEY, F.T_STARTTIME, F.T_ENDTIME, F.T_ORGID, F.T_FAULTDESC_CN, F.T_FAULTDESC_EN, F.T_TYPEID, F.T_PLANTDESC, u.T_UNITDESC_CN from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, pl.T_PLANTDESC, C.T_FAULTDESC_CN, C.T_FAULTDESC_EN, C.T_TYPEID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID = C.T_FAULTID AND B.T_MACHINEID = C.T_MACHINEID left join T_BASE_DISTRICT ta on B.T_DISTRICTID = ta.T_DISTRICTID left join T_INFO_PLANT pl on ta.T_ORGID = pl.T_ORGID where A.T_ORGID in (select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in (" + stationId + "))) and A.T_ENDTIME is null) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID)ft left join T_BASE_FAULTTYPE bt on ft.T_TYPEID = bt.T_TYPEID";
            else
                strSql = @"select ft.ID_KEY, ft.T_ENDTIME, ft.T_FAULTDESC_CN, ft.T_FAULTDESC_EN, ft.T_ORGID, ft.T_STARTTIME, ft.T_UNITDESC_CN, bt.T_TYPEDESC, ft.T_PLANTDESC From (select F.ID_KEY, F.T_STARTTIME, F.T_ENDTIME, F.T_ORGID, F.T_FAULTDESC_CN, F.T_FAULTDESC_EN, F.T_TYPEID, F.T_PLANTDESC, u.T_UNITDESC_CN from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, pl.T_PLANTDESC, C.T_FAULTDESC_CN, C.T_FAULTDESC_EN, C.T_TYPEID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID = C.T_FAULTID AND B.T_MACHINEID = C.T_MACHINEID left join T_BASE_DISTRICT ta on B.T_DISTRICTID = ta.T_DISTRICTID left join T_INFO_PLANT pl on ta.T_ORGID = pl.T_ORGID where A.T_ORGID in (select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID='" + stationId + "')) and A.T_ENDTIME is null) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID)ft left join T_BASE_FAULTTYPE bt on ft.T_TYPEID = bt.T_TYPEID";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }
        #endregion

        #region 获取电站当前故障信息(根据故障类型查询)
        public DataTable GetTotalFaultDetailInfo(string stationId, int count, int pageIndex, string type)
        {
            if (stationId.IndexOf("'") > -1)
                strSql = @"select ft.ID_KEY, ft.T_ENDTIME,ft.T_FAULTDESC_CN,ft.T_FAULTDESC_EN,ft.T_ORGID,ft.T_STARTTIME,ft.T_UNITDESC_CN,bt.T_TYPEDESC,ft.T_PLANTDESC From (select top " + count + @" F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_TYPEID,F.T_PLANTDESC,u.T_UNITDESC_CN from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, pl.T_PLANTDESC,C.T_FAULTDESC_CN, C.T_FAULTDESC_EN, C.T_TYPEID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID = C.T_FAULTID AND B.T_MACHINEID = C.T_MACHINEID left join T_BASE_DISTRICT ta on B.T_DISTRICTID = ta.T_DISTRICTID left join T_INFO_PLANT pl on ta.T_ORGID = pl.T_ORGID where A.T_ORGID in (select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in (" + stationId + @"))) and A.T_ENDTIME is null and A.ID_KEY not in(select top " + count * (pageIndex - 1) + @" ID_KEY from T_INFO_FAULTDETAIL where T_ORGID in (select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in (" + stationId + @"))) order by ID_KEY DESC) and T_ENDTIME is null) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID ORDER BY F.ID_KEY desc)ft left join T_BASE_FAULTTYPE bt on ft.T_TYPEID = bt.T_TYPEID where bt.T_TYPEDESC='" + type + "'";
            else
                strSql = @"select ft.ID_KEY, ft.T_ENDTIME,ft.T_FAULTDESC_CN,ft.T_FAULTDESC_EN,ft.T_ORGID,ft.T_STARTTIME,ft.T_UNITDESC_CN,bt.T_TYPEDESC,ft.T_PLANTDESC From (select top " + count + @" F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_TYPEID,F.T_PLANTDESC,u.T_UNITDESC_CN from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, pl.T_PLANTDESC,C.T_FAULTDESC_CN, C.T_FAULTDESC_EN, C.T_TYPEID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID = C.T_FAULTID AND B.T_MACHINEID = C.T_MACHINEID left join T_BASE_DISTRICT ta on B.T_DISTRICTID = ta.T_DISTRICTID left join T_INFO_PLANT pl on ta.T_ORGID = pl.T_ORGID where A.T_ORGID in (select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID ='" + stationId + @"')) and A.T_ENDTIME is null and A.ID_KEY not in(select top " + count * (pageIndex - 1) + @" ID_KEY from T_INFO_FAULTDETAIL where T_ORGID in (select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID ='" + stationId + @"')) order by ID_KEY DESC) and T_ENDTIME is null) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID ORDER BY F.ID_KEY desc)ft left join T_BASE_FAULTTYPE bt on ft.T_TYPEID = bt.T_TYPEID where bt.T_TYPEDESC='" + type + "'";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }



        #endregion

        #region 获取场站实时电量
        /// <summary>
        /// 功能描述：获取场站实时电量
        /// 创    建：胡进财  日    期：20141009
        /// <summary>
        /// <param name="id">电厂编码</param>
        /// <returns></returns>
        public double GetStationRealDl(string id)
        {
            strSql = "select T_ELECTAG from T_BASE_POINTS_ORG where T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });

            if (obj != null && obj != DBNull.Value)
                if (rtdbJudge == "1")
                    return GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] { obj.ToString() })), 2);
                else
                    return GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValueNoJudge", new object[] { obj.ToString() })), 2);

            else
                return double.NaN;
        }
        #endregion

        #region 获取场站容量 单个电站
        /// <summary>
        /// 功能描述：获取场站容量
        /// 创    建：胡进财  日    期：20141009
        /// <summary>
        /// <param name="id">电厂编码</param>
        /// <returns></returns>
        public double GetStationRL(string id)
        {
            double dlVal = 0;
            strSql = string.Format(@"select D_RL from (
                        select* from T_INFO_RL where T_ORGID in (
                        select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in
                        (select T_ORGID from T_INFO_PLANT where T_ORGID = '{0}')
                        )) t where not exists(select 1 from(
                        select * from T_INFO_RL where T_ORGID in (
                        select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in
                        (select T_ORGID from T_INFO_PLANT where T_ORGID = '{0}')
                        )) t1 where t1.T_ORGID = t.T_ORGID and t1.T_TIME > t.T_TIME)", id);
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            if (obj != null)
                return Convert.ToDouble(obj);
            return dlVal;
        }
        #endregion

        #region 批量获取场站容量 所有电站
        /// <summary>
        /// 功能描述：批量获取场站容量
        /// 创    建：刘海杰  日    期：20160825
        /// <summary>
        /// <param name="id">电厂编码</param>
        /// <returns></returns>
        public DataTable GetAllStationRL(string id)
        {
            string str_order = id.Replace("(", "").Replace(")", "").Replace("'", "");
            strSql = @"select b.T_ORGID,a.T_ORGID as T_DISTRICTID,a.t_time,a.d_rl from
            T_INFO_RL as a inner join (select top 100 percent  T_DISTRICTID, T_ORGID from T_BASE_DISTRICT where
            T_ORGID in (" + id + ") order by CHARINDEX(',' + LTRIM(T_ORGID) + ',', '," + str_order + ",'))  as b on b.T_DISTRICTID = a.T_ORGID order by t_time desc";


            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });

            return dt;
        }
        #endregion

        #region  获取实时点数据
        public List<double[]> GetRealVal(string id, string[] name)
        {
            List<double[]> list = new List<double[]>();
            string column = "";
            string[] points = null;
            double[] val = null;
            for (int i = 0; i < name.Length; i++)
            {
                if (name[i] == "负荷")//并网功率
                {
                    column += "T_POWERTAG,";
                }
                else if (name[i] == "电量")
                {
                    column += "T_ELECTAG,";
                }
                else if (name[i] == "辐照度")
                {
                    column += "T_IRRADIANCETAG,";
                }
                else if (name[i] == "湿度")
                {
                    column += "T_TEMPTAG,";
                }
                else if (name[i] == "温度")
                {
                    column += "T_HUMIDITYTAG,";
                }
                else if (name[i] == "状态")
                {
                    column += "T_STATUSTAG,";
                }
                else if (name[i] == "累计电量")
                {
                    column += "T_TOTALELECTAG,";
                }
                else if (name[i] == "离散率")
                {
                    column += "T_DISPERSIONRATIOTAG,";
                }
                else if (name[i] == "交流侧功率")
                {
                    column += "T_ACPOWERTAG,";
                }
                else if (name[i] == "直流侧功率")
                {
                    column += "T_DCPOWERTAG,";
                }
                else if (name[i] == "集电侧功率")
                {
                    column += "T_CollectorRegionTAG,";
                }
                else if (name[i] == "理论功率")
                {
                    column += "T_THEORYTAG,";
                }

            }

            if (column != "")
            {
                List<string> _points = new List<string>();
                column = column.Substring(0, column.Length - 1);

                string str_order = id.Replace("(", "").Replace(")", "").Replace("'", "");
                //strSql = "select T_POWERTAG from T_BASE_POINTS_ORG where T_ORGID in  (" + id + ") order by CHARINDEX(',' + LTRIM(T_ORGID) + ',', '," + str_order + ",')";
                strSql = "select " + column + " from T_BASE_POINTS_ORG where T_ORGID in(" + id + ") order by CHARINDEX(',' + LTRIM(T_ORGID) + ',', '," + str_order + ",')";



                dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
                if (dt != null && dt.Rows.Count > 0)
                {
                    points = new string[dt.Rows.Count];
                    val = new double[dt.Rows.Count];
                    for (int i = 0; i < name.Length; i++)
                    {
                        for (int k = 0; k < dt.Rows.Count; k++)
                        {
                            _points.Add(dt.Rows[k][i].ToString());

                        }
                    }
                    if (rtdbJudge == "1")
                        val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValues", new object[] { _points.ToArray() });
                    else
                        val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValuesNoJudge", new object[] { _points.ToArray() });

                    List<double> list_val = new List<double>(val);

                    for (int i = 0; i < name.Length; i++)
                    {
                        val = new double[dt.Rows.Count];
                        for (int k = 0; k < dt.Rows.Count; k++)
                        {
                            val[k] = list_val[0];
                            list_val.RemoveAt(0);
                        }
                        list.Add(val);
                    }
                }
            }
            return list;
        }

        #endregion

        #region 报表

        #region 电量报表
        public List<double[]> GetRealVal(string id)
        {
            List<double[]> list = new List<double[]>();
            //      string column = "";
            //      string[] points = null;
            //      double[] val = null;
            //      //for (int i = 0; i < name.Length; i++)
            //      //{
            //      //    if (name[i] == "负荷")
            //      //    {
            //      //        column += "T_POWERTAG,";
            //      //    }
            //      //    else if (name[i] == "电量")
            //      //    {
            //      //        column += "T_ELECTAG,";
            //      //    }
            //      //    else if (name[i] == "辐照度")
            //      //    {
            //      //        column += "T_IRRADIANCETAG,";
            //      //    }
            //      //    else if (name[i] == "湿度")
            //      //    {
            //      //        column += "T_TEMPTAG,";
            //      //    }
            //      //    else if (name[i] == "温度")
            //      //    {
            //      //        column += "T_HUMIDITYTAG,";
            //      //    }
            //      //    else if (name[i] == "状态")
            //      //    {
            //      //        column += "T_STATUSTAG,";
            //      //    }
            //      //    else if (name[i] == "累计电量")
            //      //    {
            //      //        column += "T_TOTALELECTAG,";
            //      //    }
            //      //}

            //      if (column != "")
            //      {
            //          List<string> _points = new List<string>();
            //          column = column.Substring(0, column.Length - 1);

            //          string str_order = id.Replace("(", "").Replace(")", "").Replace("'", "");

            //          strSql = @"SELECT [ID_KEY],[T_ORGID]
            //,[T_POWERTAG]
            //,[T_HUMIDITYTAG]
            //,[T_ELECTAG]
            //,[T_IRRADIANCETAG]
            //,[T_TEMPTAG]
            //,[T_STATUSTAG]
            //,[T_TOTALELECTAG]
            //,[T_GRIDPOWERTAG]
            //,[T_ETPJTAG]
            //,[T_ETPFTAG]
            //,[T_ETPPTAG]
            //,[T_ETPGTAG]
            //,[T_FETPTAG]
            //,[T_FETPJTAG]
            //,[T_FETPFTAG]
            //,[T_FETPPTAG]
            //,[T_FETPGTAG]
            //,[T_EQTTTAG]
            //,[T_EQTJTAG]
            //,[T_EQTFTAG]
            //,[T_EQTPTAG]
            //,[T_EQTGTAG]
            //,[T_FEQTTTAG]
            //,[T_FEQTJTAG]
            //,[T_FEQTFTAG]
            //,[T_FEQTPTAG]
            //,[T_FEQTGTAG]
            //  FROM[Monitoring].[dbo].[T_BASE_POINTS_ORG]";



            //          dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            //          if (dt != null && dt.Rows.Count > 0)
            //          {
            //              points = new string[dt.Rows.Count];
            //              val = new double[dt.Rows.Count];
            //              for (int i = 0; i < name.Length; i++)
            //              {
            //                  for (int k = 0; k < dt.Rows.Count; k++)
            //                  {
            //                      _points.Add(dt.Rows[k][i].ToString());

            //                  }
            //              }
            //              if (rtdbJudge == "1")
            //                  val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValues", new object[] { _points.ToArray() });
            //              else
            //                  val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValuesNoJudge", new object[] { _points.ToArray() });

            //              List<double> list_val = new List<double>(val);

            //              for (int i = 0; i < name.Length; i++)
            //              {
            //                  val = new double[dt.Rows.Count];
            //                  for (int k = 0; k < dt.Rows.Count; k++)
            //                  {
            //                      val[k] = list_val[0];
            //                      list_val.RemoveAt(0);
            //                  }
            //                  list.Add(val);
            //              }
            //          }
            //      }
            return list;
        }
        #endregion

        #endregion

        #region 获取场站名字
        /// <summary>
        /// 功能描述：获取场站
        /// 创    建：胡进财  日    期：20141009
        /// <summary>
        /// <param name="id">电厂编码</param>
        /// <returns></returns>
        public string GetStationName(string id)
        {
            strSql = "select T_PLANTDESC from T_INFO_PLANT where T_ORGID = '" + id + "'";//由于组织机构变更，查询方法修改

            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });

            if (dt != null)
            {
                plantName = dt.Rows[0]["T_PLANTDESC"].ToString();
            }
            return plantName;
        }
        #endregion

        #region 判断电站是否存在
        public bool Check(string id)
        {
            strSql = "select count(*) from T_INFO_PLANT where T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });

            if (obj.ToString() != "0")
                return true;
            else
                return false;
        }
        #endregion

        #region 获取电站负荷数据
        /// <summary>
        /// 功能描述：获取电站负荷数据
        /// 创    建：胡进财  日    期：20141009
        /// <summary>
        /// <param name="id">电厂编码</param>
        /// <returns></returns>
        public object GetStationPower(string id)
        {
            strSql = "select T_POWERTAG from T_BASE_POINTS_ORG where T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });

            if (obj != null && obj != DBNull.Value)
                if (rtdbJudge == "1")
                    dlVal = GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] { obj.ToString() })), 2);
                else
                    dlVal = GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValueNoJudge", new object[] { obj.ToString() })), 2);
            return dlVal;
        }

        public double[] GetStationPowerArray(string id)
        {
            double[] val = null;
            string str_order = id.Replace("(", "").Replace(")", "").Replace("'", "");
            strSql = "select T_POWERTAG from T_BASE_POINTS_ORG where T_ORGID in  (" + id + ") order by CHARINDEX(',' + LTRIM(T_ORGID) + ',', '," + str_order + ",')";



            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });

            string[] str_arr = dt.AsEnumerable().Select(t => t.Field<string>("T_POWERTAG")).ToArray();
            if (str_arr != null)
                if (rtdbJudge == "1")
                    val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValues", new object[] { str_arr });
                else
                    val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValuesNoJudge", new object[] { str_arr });
            return val;
        }
        #endregion

        #region 获取电站天气状态数据
        /// <summary>
        /// 功能描述：获取电站天气状态数据
        /// 创    建：胡进财  日    期：20141009
        /// <summary>
        /// <param name="id">电厂编码</param>
        /// <returns></returns>
        public int GetStationState(string id)
        {
            strSql = "select T_STATE from T_BASE_POINTS_ORG where T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });

            if (obj != null && obj != DBNull.Value)
                if (rtdbJudge == "1")
                    dlVal = iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] { obj.ToString() });
                else
                    dlVal = iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValueNoJudge", new object[] { obj.ToString() });

            return Convert.ToInt32(dlVal);
        }
        #endregion


        #region 获取多个电站下的逆变器
        public DataTable GetInverters(string id)
        {
            if (id.IndexOf("'") > -1)
                strSql = @"SELECT C.I_ORDER,A.T_ORGID ,B.T_ORGID ,T_DISTRICTDESC_CN ,B.T_DISTRICTID ,
                                T_UNITDESC_CN ,C.T_UNITID ,C.T_MACHINEID,count(*) Total FROM T_INFO_PLANT A
                    INNER JOIN T_BASE_DISTRICT B ON A.T_ORGID=B.T_ORGID
                    INNER JOIN T_BASE_UNIT C ON B.T_DISTRICTID=C.T_DISTRICTID left join T_BASE_STRING S on C.T_UNITID=S.T_UNITID
                    WHERE A.T_ORGID IN (" + id + ") and  C.T_MACHINEID in(select T_MACHINEID from T_BASE_MACHINE where T_TYPEID='nbq') group by A.T_ORGID ,B.T_ORGID ,T_DISTRICTDESC_CN ,B.T_DISTRICTID ,T_UNITDESC_CN ,C.T_UNITID ,C.T_MACHINEID,C.I_ORDER ORDER BY C.I_ORDER";
            else
                strSql = @"SELECT C.I_ORDER,A.T_ORGID ,B.T_ORGID ,T_DISTRICTDESC_CN ,B.T_DISTRICTID ,
                                T_UNITDESC_CN ,C.T_UNITID ,C.T_MACHINEID,count(*) Total FROM T_INFO_PLANT A
                    INNER JOIN T_BASE_DISTRICT B ON A.T_ORGID=B.T_ORGID
                    INNER JOIN T_BASE_UNIT C ON B.T_DISTRICTID=C.T_DISTRICTID left join T_BASE_STRING S on C.T_UNITID=S.T_UNITID
                    WHERE A.T_ORGID='" + id + "' and  C.T_MACHINEID in(select T_MACHINEID from T_BASE_MACHINE where T_TYPEID='nbq') group by A.T_ORGID ,B.T_ORGID ,T_DISTRICTDESC_CN ,B.T_DISTRICTID ,T_UNITDESC_CN ,C.T_UNITID ,C.T_MACHINEID,C.I_ORDER ORDER BY C.I_ORDER";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }
        #endregion

        #region 获取场站实时电量
        /// <summary>
        /// 功能描述：获取场站实时电量
        /// 创    建：胡进财  日    期：20141009
        /// <summary>
        /// <param name="id">电厂编码</param>
        /// <returns></returns>
        public double[] GetStationHisDl(string id, DateTime st, DateTime et)
        {
            double[] val = new double[1] { 0 };
            strSql = "select T_ELECTAG from T_BASE_POINTS_ORG where T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });

            if (obj != null && obj != DBNull.Value)
            {

                val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValueByStartTimeAndEndTime", new object[] { obj.ToString(), st, et });

            }
            return val;
        }

        public List<double[]> GetArrayHisDl(string id, DateTime st)
        {
            string[] _str = new string[2];
            double[] d_vals = new double[1];
            List<double[]> val = new List<double[]>();
            string str_order = id.Replace("(", "").Replace(")", "").Replace("'", "");
            strSql = "select T_IRRADIANCETAG,T_ELECTAG from T_BASE_POINTS_ORG where T_ORGID in  (" + id + ") order by CHARINDEX(',' + LTRIM(T_ORGID) + ',', '," + str_order + ",')";

            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });

            if (dt != null)
            {
                string[] str_arr = dt.AsEnumerable().Select(t => t.Field<string>("T_ELECTAG")).ToArray();
                //if (str_arr != null)
                //    val.Add((double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValues", new object[] { str_arr, st }));

                //str_arr = dt.AsEnumerable().Select(t => t.Field<string>("T_IRRADIANCETAG")).ToArray();
                //if (str_arr != null)
                //    val.Add((double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValues", new object[] { str_arr, st }));


                List<string> l_data = new List<string>();
                List<string> l_data1 = new List<string>(str_arr);
                double[] d_val1 = new double[l_data1.Count];
                l_data.AddRange(l_data1);
                string[] str_arr2 = dt.AsEnumerable().Select(t => t.Field<string>("T_IRRADIANCETAG")).ToArray();
                l_data1 = new List<string>(str_arr2);
                double[] d_val2 = new double[l_data1.Count];
                l_data.AddRange(l_data1);

                object d_vals1 = iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValues", new object[] { l_data.ToArray(), st });
                d_vals = (double[])d_vals1;
                for (int i = 0; i < d_val1.Length; i++)
                {
                    d_val1[i] = d_vals[i];
                }

                int num = 0;
                for (int i = d_val1.Length; i < d_val1.Length + d_val2.Length; i++)
                {
                    d_val2[num] = d_vals[i];
                    num++;
                }
                val.Add(d_val1);
                val.Add(d_val2);
            }

            return val;
        }

        #endregion

        #region 获取场站电量
        /// <summary>
        /// 获取场站电量
        /// </summary>
        /// <param name="key>电场编码param>
        /// <param name="st">开始时间</param>
        /// <param name="et">结束时间</param>
        /// <returns></returns>
        public double GetStationDL(string key, string st, string et)
        {
            strSql = "select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID ='" + key + "'";
            //获取工期编码
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            strSql = "";
            strSql += "select sum(val) from ";
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i == 0)
                        strSql += "(select sum(D_VALUE) val from T_INFO_STATISTICS where T_TJID='DL' and T_ORGID='" + dt.Rows[i][0].ToString() + "' and (T_TIME between '" + st + "' and '" + et + "')";
                    else
                        strSql += "union  (select sum(D_VALUE) val from T_INFO_STATISTICS where T_TJID='DL' and T_ORGID='" + dt.Rows[i][0].ToString() + "' and (T_TIME between '" + st + "' and '" + et + "'))";
                }
                strSql += ") a";

                obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });

                if (obj != null && obj != DBNull.Value)
                {
                    dlVal = Convert.ToDouble(Convert.ToDouble(obj).ToString("f2"));
                }
            }
            return Convert.ToDouble(Convert.ToDouble(dlVal).ToString("f2"));
        }
        #endregion

        #region 批量获取场站电量
        /// <summary>
        /// 批量获取场站电量
        /// </summary>
        /// <param name="key>电场编码param>
        /// <param name="st">开始时间</param>
        /// <param name="et">结束时间</param>
        /// <returns></returns>
        public DataTable GetArrayStationDL(string key, string st, string et)
        {

            strSql = "select T_BASE_DISTRICT.T_ORGID, sum(isnull(D_VALUE, 0)) val from T_INFO_STATISTICS " +
   "inner join T_BASE_DISTRICT on T_INFO_STATISTICS.T_ORGID =T_BASE_DISTRICT.T_DISTRICTID " +
   " and T_INFO_STATISTICS.T_ORGID in (select T_DISTRICTID From T_BASE_DISTRICT where T_ORGID in (" + key + ")) and T_INFO_STATISTICS.T_TJID = 'DL' and " +
   "(T_TIME between '" + st + "' and '" + et + "') group by T_BASE_DISTRICT.T_ORGID";

            //获取工期编码
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });

            return dt;
        }
        #endregion


        public DataTable GetPointOrgInfo(string id)
        {
            strSql = "select * From T_BASE_POINTS_ORG where T_ORGID like'%" + id + "%'";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }

        #region 按月统计电站电量
        public double[] GetGroupDL(string key, string st, string et)
        {

            strSql = "select  DATEPART(month,T_TIME)   T_TIMES, sum(isnull(D_VALUE, 0))  val from " +
            "T_INFO_STATISTICS  inner join T_BASE_DISTRICT on T_INFO_STATISTICS.T_ORGID = T_BASE_DISTRICT.T_DISTRICTID " +
            " and T_INFO_STATISTICS.T_ORGID in (select T_DISTRICTID From T_BASE_DISTRICT where T_ORGID in (" + key + ")) " +
            "and T_INFO_STATISTICS.T_TJID = 'DL' and(T_TIME between '" + st + "' and '" + et + "') " +
            "group by   DATEPART(month, T_TIME) order by T_TIMES asc";

            //获取工期编码
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            double[] d_dl = new double[1];
            if (dt != null)
            {
                d_dl = new double[12];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    d_dl[Convert.ToInt32(dt.Rows[i][0].ToString())] = Convert.ToDouble(dt.Rows[i][1].ToString());
                }
            }

            return d_dl;
        }
        #endregion

        #region 累计发电量
        public double SumDL(string id)
        {
            //if (id.IndexOf("'") > -1)
            //    strSql = "select isnull(sum(D_VALUE),0) val from T_INFO_STATISTICS where T_TJID='DL' and T_ORGID in(select T_DISTRICTID From T_BASE_DISTRICT where T_ORGID in (" + id + "))";
            //else
            //    strSql = "select isnull(sum(D_VALUE),0) val from T_INFO_STATISTICS where T_TJID='DL' and T_ORGID in(select T_DISTRICTID From T_BASE_DISTRICT where T_ORGID ='" + id + "')";
            //dlVal = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            //return double.Parse(dlVal.ToString());
            strSql = "select T_TOTALELECTAG from T_BASE_POINTS_ORG where T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            double val = 0;
            if (obj != null && obj != DBNull.Value)
                if (rtdbJudge == "1")
                    val = GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] { obj.ToString() })), 2);
                else
                    val = GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValueNoJudge", new object[] { obj.ToString() })), 2);
            return val;
        }
        #endregion

        #region 节约标煤量
        public double GetJM(string id)
        {
            return GetDouble(GetSumDL(id) / 1000 * 0.36, 2);
        }
        #endregion

        #region 二氧化碳减排量
        public double GetEYHTJP(string id)
        {
            return GetDouble(GetSumDL(id) / 1000 * 0.997, 2);
        }
        #endregion

        #region 获取场站温度
        /// <summary>
        /// 功能描述：获取场站温度
        /// 创    建：胡进财  日    期：20141009
        /// </summary>
        /// <param name="id">电厂编码</param>
        /// <returns></returns>
        public object GetStationTemperature(string id)
        {
            strSql = "select t_temptag from T_BASE_POINTS_ORG where T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            if (obj != null && obj != DBNull.Value)
                if (rtdbJudge == "1")
                    dlVal = Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] { obj.ToString() }));
                else
                    dlVal = Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValueNoJudge", new object[] { obj.ToString() }));

            return dlVal;
        }
        #endregion

        #region 获取场站湿度
        /// <summary>
        /// 功能描述：获取场站湿度
        /// 创    建：胡进财  日    期：20141009
        /// </summary>
        /// <param name="id">场站编码</param>
        /// <returns></returns>
        public object GetStationHumidity(string id)
        {
            strSql = "select t_humiditytag from T_BASE_POINTS_ORG where T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            if (obj != null && obj != DBNull.Value)
                if (rtdbJudge == "1")
                    dlVal = GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] { obj.ToString() })), 2);
                else
                    dlVal = GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValueNoJudge", new object[] { obj.ToString() })), 2);

            return dlVal;
        }
        #endregion

        #region 辐照度
        /// 功能描述：获取场站辐照度
        /// 创    建：胡进财  日    期：20141009
        /// </summary>
        /// <param name="key">编码</param>
        /// <returns></returns>
        public object GetStationIrradiance(string key)
        {
            strSql = "select t_irradiancetag from T_BASE_POINTS_ORG where T_ORGID='" + key + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            if (obj != null && obj != DBNull.Value)
                if (rtdbJudge == "1")
                    dlVal = GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] { obj.ToString() })), 2);
                else
                    dlVal = GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValueNoJudge", new object[] { obj.ToString() })), 2);

            return dlVal;
        }
        #endregion

        #region 辐照度趋势
        public double[] GetIrradianceLine(string tag, DateTime sTime, DateTime eTime, int second)
        {
            double[] val = null;
            strSql = "select t_irradiancetag from T_BASE_POINTS_ORG where T_ORGID='" + tag + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });

            if (obj != null && obj != DBNull.Value)
                val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValueByStep", new object[] { obj.ToString(), sTime, eTime, second });
            return val;
        }
        #endregion

        #region 离散率趋势
        public double[] GetDispersionRatioLine(string tag, DateTime sTime, DateTime eTime, int second)
        {
            double[] val = null;
            strSql = "select T_DISPERSIONRATIOTAG from T_BASE_POINTS_ORG where T_ORGID='" + tag + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });

            if (obj != null && obj != DBNull.Value)
                val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValueByStep", new object[] { obj.ToString(), sTime, eTime, second });
            return val;
        }
        #endregion


        #region 逆变器数量
        /// 功能描述：获取场站逆变器数量
        /// 创    建：胡进财  日    期：20141009
        /// </summary>
        /// <param name="key">编码</param>
        /// <returns></returns>
        public int GetStationInverterCount(string id)
        {
            int count = 0;
            strSql = "select count(*) from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = '" + id + "') and T_MACHINEID in(select T_MACHINEID from T_BASE_MACHINE where T_TYPEID='nbq')";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            if (obj != null && obj != DBNull.Value)
                count = Convert.ToInt32(obj);
            return count;
        }
        #endregion

        #region 获取场站区域信息
        /// <summary>
        /// 获取场站区域信息
        /// </summary>
        /// <param name="id">场站编码</param>
        /// <returns></returns>
        public DataTable GetStationInverterInfo(string id)
        {
            strSql = "select T_DISTRICTID,T_DISTRICTDESC_CN,T_DISTRICTDESC_EN from T_BASE_DISTRICT where T_ORGID='" + id + "'";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }
        #endregion

        #region 获取电站信息
        /// <summary>
        /// 获取电站信息
        /// </summary>
        /// <param name="userId">用户编码</param>
        /// <returns></returns>
        public DataTable GetStations(string userId)
        {
            DataTable dt = new DataTable();
            //由于组织机构进行修改，插叙方法修改
            //strSql = "select T_ORGID from T_BASE_ORG where T_PARENTID in(select T_ORGID from T_SYS_MEMBERORG where T_USERID='" + userId + "')";
            strSql = "select T_ORGID from T_INFO_PLANT where T_PARENTID in(select T_ORGID from T_SYS_MEMBERORG where T_USERID='" + userId + "')";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            if (dt != null & dt.Rows.Count > 0)
            {
                strSql = "select T_ORGID from T_INFO_PLANT";

                DataTable dtStations = new DataTable();

                dtStations = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });

                GetList(dt, dtStations);
            }
            stationId = stationId.Substring(0, stationId.Length - 1);
            strSql = "select s.T_ORGID,s.T_ORGDESC_CN,s.T_ORGDESC_EN,s.T_INSTALLDATA,s.D_LAT,s.D_LONG,t.T_POWERTAG,t.T_ELECTAG from (";
            strSql += " select org.T_ORGID,org.T_ORGDESC_CN,org.T_ORGDESC_EN,pl.T_INSTALLDATA,pl.D_LAT,pl.D_LONG from T_BASE_ORG org inner join T_INFO_PLANT pl";
            strSql += " on org.T_ORGID = pl.T_ORGID) s inner join T_BASE_POINTS_ORG t on s.T_ORGID=t.T_ORGID where s.T_ORGID in(" + stationId + ");";

            stationId = "";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });

            DataTable tblDatas = new DataTable("Station");
            DataColumn dc = null;

            //赋值给dc，是便于对每一个datacolumn的操作
            dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
            dc.AutoIncrement = true;//自动增加
            dc.AutoIncrementSeed = 1;//起始为1
            dc.AutoIncrementStep = 1;//步长为1
            dc.AllowDBNull = false;//

            dc = tblDatas.Columns.Add("stationId", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("stationNameCN", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("stationNameEN", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("data", Type.GetType("System.String"));
            dc = tblDatas.Columns.Add("power", Type.GetType("System.Double"));
            dc = tblDatas.Columns.Add("rl", Type.GetType("System.Double"));
            dc = tblDatas.Columns.Add("dl", Type.GetType("System.Double"));
            dc = tblDatas.Columns.Add("lat", Type.GetType("System.Double"));
            dc = tblDatas.Columns.Add("long", Type.GetType("System.Double"));
            DataRow dr;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                strSql = "select T_DISTRICTID From T_BASE_DISTRICT where T_ORGID='" + dt.Rows[i]["T_ORGID"] + "'";
                DataTable dtD = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });

                double rl = 0;
                for (int k = 0; k < dtD.Rows.Count; k++)
                {
                    strSql = " select top 1 D_RL From T_INFO_RL where T_ORGID='" + dtD.Rows[k][0] + "' order by T_TIME desc";
                    object obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
                    if (obj != null && obj != DBNull.Value)
                        rl += Convert.ToDouble(obj);
                }

                dr = tblDatas.NewRow();
                dr["stationId"] = dt.Rows[i]["T_ORGID"].ToString();
                dr["stationNameCN"] = dt.Rows[i]["T_ORGDESC_CN"].ToString();
                dr["stationNameEN"] = dt.Rows[i]["T_ORGDESC_EN"].ToString();
                if (rtdbJudge == "1")
                    dr["power"] = iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] { dt.Rows[i]["T_POWERTAG"].ToString() });
                else
                    dr["power"] = iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValueNoJudge", new object[] { dt.Rows[i]["T_POWERTAG"].ToString() });


                dr["lat"] = dt.Rows[i]["D_LAT"].ToString();
                dr["data"] = dt.Rows[i]["T_INSTALLDATA"].ToString();
                dr["long"] = dt.Rows[i]["D_LONG"].ToString();
                dr["rl"] = rl;

                if (rtdbJudge == "1")
                    dr["dl"] = iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] { dt.Rows[i]["T_ELECTAG"].ToString() });
                else
                    dr["dl"] = iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValueNoJudge", new object[] { dt.Rows[i]["T_ELECTAG"].ToString() });

                tblDatas.Rows.Add(dr);
            }
            return tblDatas;
        }

        private static string stationId = "";
        public void GetList(DataTable dtOrg, DataTable dtStation)
        {
            string orgids = "";
            for (int i = 0; i < dtOrg.Rows.Count; i++)
            {
                bool judge = false;
                for (int k = 0; k < dtStation.Rows.Count; k++)
                {
                    if (dtOrg.Rows[i][0].ToString().Trim() == dtStation.Rows[k][0].ToString().Trim())
                    {
                        judge = true;
                        stationId += "'" + dtStation.Rows[k][0].ToString() + "',";
                        break;
                    }
                }
                if (judge == false)
                    orgids += "'" + dtOrg.Rows[i][0] + "',";
            }
            DataTable dt = new DataTable();
            if (orgids != "")
            {
                orgids = orgids.Substring(0, orgids.Length - 1);
                strSql = "select T_ORGID from T_BASE_ORG where T_PARENTID in(" + orgids + ")";
                dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
                GetList(dt, dtStation);
            }
        }
        #endregion


        #region 电站基本测点
        public DataTable GetStationPoint(string ids)
        {
            strSql = "select T_POWERTAG,T_TEMPTAG from T_BASE_POINTS_ORG where T_ORGID in(" + ids + ")";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }
        #endregion

        #region 电站状态
        public int GetPowerPlantState(string id)
        {
            int index = 0;

            strSql = "select t_statustag from T_BASE_POINTS_ORG where T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });

            if (obj != null && obj != DBNull.Value)
            {
                if (rtdbJudge == "1")
                    obj = Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] { obj.ToString() }));
                else
                    obj = Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValueNoJudge", new object[] { obj.ToString() }));
                if (obj != null && !double.IsNaN((double)obj))
                    index = Convert.ToInt32(obj);
                else
                    index = 3;
            }

            return index;

            //根据电站ID(T_ORGID)在t_base_points_org中获取t_statustatus测点名，根据测点在实时库中获取实时状态值

            //double dVal = 0;
            ////strSql = "select D_LEVEL1,D_LEVEL2,D_LEVEL3 from T_INFO_PLANT where T_ORGID='" + id + "'";
            ////dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql,strErrMsg });
            ////return dt;
            //strSql = "select t_irradiancetag from T_BASE_POINTS_ORG where T_ORGID='" + id + "'";
            //obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            //if (obj != null && obj != DBNull.Value && obj != "")
            //    dVal = GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] {obj.ToString()})), 2);
            //if (dVal <= 200)
            //    index = 0;
            //else {
            //    double dPower = 0;

            //    strSql = "select D_STATIONWARINGLEVEL1 from T_BASE_SYSPARA";
            //    obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            //    double Halt = 0;
            //    if (obj != DBNull.Value)
            //        Halt = double.Parse(obj.ToString());

            //    strSql = "select D_STATIONWARINGLEVEL2 from T_BASE_SYSPARA";
            //    obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            //    double Waring = 0;
            //    if (obj != DBNull.Value)
            //        Waring = double.Parse(obj.ToString());

            //    strSql = "select T_POWERTAG from T_BASE_POINTS_ORG where T_ORGID='" + id + "'";
            //    obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });

            //    if (obj != null && obj != DBNull.Value && obj != "")
            //        dlVal = GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] {obj.ToString()})), 2);
            //    if (dlVal != null)
            //        dPower = double.Parse(dlVal.ToString());
            //    if (dPower <= Halt * 0.05)
            //        index = 1;
            //    else if (dPower <= Waring * 0.6 && dPower > Halt * 0.05)
            //        index = 2;
            //    else if (dPower > Waring * 0.6)
            //        index = 3;
            //}
            //return index;
        }
        #endregion

        #region 发电效率
        public object GetStationEfficiency(string id)
        {
            if (GetStationRL(id) != 0 && GetStationIrradiance(id) != null)
                if (GetStationPower(id) != null && GetStationPower(id) != null && GetStationIrradiance(id) != null)

                    dlVal = GetDouble(Convert.ToDouble(Convert.ToDouble(GetStationPower(id))) * 1000 / (GetStationRL(id) * Convert.ToDouble(GetStationIrradiance(id))) * 100, 2);

            return GetDouble(Convert.ToDouble(dlVal), 2);
        }
        #endregion

        #region 电站趋势
        //并网功率
        public double[] GetPowerPlantLine(string tag, DateTime sTime, DateTime eTime, int second)
        {
            double[] val = null;
            strSql = "select T_POWERTAG from T_BASE_POINTS_ORG where T_ORGID='" + tag + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });

            if (obj != null && obj != DBNull.Value)
                val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValueByStep", new object[] { obj.ToString(), sTime, eTime, second });
            return val;
        }
        //直流功率
        public double[] GetDCPowerLine(string tag, DateTime sTime, DateTime eTime, int second)
        {
            double[] val = null;
            strSql = "select T_DCPOWERTAG from T_BASE_POINTS_ORG where T_ORGID='" + tag + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });

            if (obj != null && obj != DBNull.Value)
                val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValueByStep", new object[] { obj.ToString(), sTime, eTime, second });
            return val;
        }
        //交流侧功率
        public double[] GetGridPowerLine(string tag, DateTime sTime, DateTime eTime, int second)
        {
            double[] val = null;
            strSql = "select T_ACPOWERTAG from T_BASE_POINTS_ORG where T_ORGID='" + tag + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });

            if (obj != null && obj != DBNull.Value)
                val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValueByStep", new object[] { obj.ToString(), sTime, eTime, second });
            return val;
        }
        public double[][] GetLine(string id, string names, DateTime sTime, DateTime eTime, int second)
        {
            double[][] val = null;
            string column = "";

            string[] name = names.Split(',');

            for (int i = 0; i < name.Length; i++)
            {
                if (name[i] == "并网功率")
                {
                    column += "T_POWERTAG,";
                }
                else if (name[i] == "直流功率")
                {
                    column += "T_DCPOWERTAG,";
                }
                else if (name[i] == "交流侧功率")
                {
                    column += "T_ACPOWERTAG,";
                }
                else if (name[i] == "辐照度")
                {
                    column += "T_IRRADIANCETAG,";
                }
            }

            column = column.Substring(0, column.Length - 1);

            strSql = "select " + column + " from T_BASE_POINTS_ORG where T_ORGID='" + id + "'";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            string[] points = new string[dt.Columns.Count];

            for (int i = 0; i < dt.Columns.Count; i++)
            {
                points[i] = dt.Rows[0][i].ToString();
            }

            val = (double[][])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValuesByStep", new object[] { points, sTime, eTime, second });

            return val;
        }
        #endregion

        #region 离散率曲线

        #endregion

        #region 获取区域信息
        public DataTable GetDistrictInfos(string id)
        {
            if (id.IndexOf("'") > -1)
                strSql = "select T_ORGID,T_DISTRICTID,T_DISTRICTDESC_CN,T_DISTRICTDESC_EN from T_BASE_DISTRICT where T_ORGID in(" + id + ")";
            else
                strSql = "select T_ORGID,T_DISTRICTID,T_DISTRICTDESC_CN,T_DISTRICTDESC_EN from T_BASE_DISTRICT where T_ORGID ='" + id + "'";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }
        #endregion

        #region 获取电站详细信息
        public DataTable GetStationInfo(string id)
        {
            strSql = "select *From T_INFO_PLANT where T_ORGID='" + id + "'";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }
        #endregion

        #region List 转换
        public IList<Hashtable> DataTableToList(DataTable dt)
        {
            IList<Hashtable> list = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                list = new List<Hashtable>();
                Hashtable ht = null;
                foreach (DataRow row in dt.Rows)
                {
                    ht = new Hashtable();
                    foreach (DataColumn col in dt.Columns)
                    {
                        if (row[col.ColumnName] != null && !string.IsNullOrEmpty(Convert.ToString(row[col.ColumnName])))
                        {
                            ht.Add(col.ColumnName.ToUpper(), row[col.ColumnName]);
                        }
                        else
                        {
                            ht.Add(col.ColumnName.ToUpper(), "");
                        }
                    }
                    list.Add(ht);
                }
            }
            return list;
        }
        #endregion

        #region 累计辐照度
        public double GetSumIrradiance(string id, string st, string et)
        {
            strSql = "select sum(D_VALUE) from T_INFO_STATISTICS where T_TJID='FZD' and (T_TIME between '" + st + "' and '" + et + "')  and T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            if (obj != null && obj != DBNull.Value)
                dlVal = Convert.ToDouble(obj);
            return Convert.ToDouble(Convert.ToDouble(dlVal).ToString("f2"));
        }
        #endregion

        #region 平均辐照度
        public double GetAvgIrradiance(string id, string st, string et)
        {
            dlVal = null;
            strSql = "select avg(D_VALUE) from T_INFO_STATISTICS where T_TJID='LJFZ' and (T_TIME between '" + st + "' and '" + et + "')  and T_ORGID=(SELECT T_DISTRICTID  FROM T_BASE_DISTRICT WHERE T_ORGID='" + id + "')";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            if (obj != null && obj != DBNull.Value)
            {
                dlVal = Convert.ToDouble(obj);
                return Convert.ToDouble(Convert.ToDouble(dlVal).ToString("f2"));
            }
            else
                return 0;
        }
        #endregion

        #region  平均功率
        public double GetAvgPower(string id, string st, string et)
        {
            strSql = "select avg(D_VALUE) from T_INFO_STATISTICS where T_TJID='PJGL' and (T_TIME between '" + st + "' and '" + et + "')  and T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            if (obj != null && obj != DBNull.Value)
                dlVal = Convert.ToDouble(obj);
            return Convert.ToDouble(Convert.ToDouble(dlVal).ToString("f2"));
        }
        #endregion

        #region 累计发电效率
        public double GetSumEfficiency(string id, string st, string et)
        {
            double rl = 0;
            //strSql = " select top 1 D_RL From T_INFO_RL where T_ORGID='" + id + "' and (T_TIME between '" + st + "' and '" + et + "') order by T_TIME desc";
            //obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            //if (obj != null && obj != DBNull.Value)
            //    rl = Convert.ToDouble(obj);
            //else

            /*需要判断该段时间内是否增加容量  如果增加需要加权平均*/
            //strSql = " select top 1 D_RL From T_INFO_RL where T_ORGID='" + id + "' and T_TIME <='" + et + "' order by T_TIME desc";

            strSql = @"select D_RL from (
                        select* from T_INFO_RL where T_ORGID in (
                        select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in
                        (select T_ORGID from T_INFO_PLANT where T_ORGID = '" + id + @"')
                        )) t where not exists(select 1 from(
                        select * from T_INFO_RL where T_ORGID in (
                        select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in
                        (select T_ORGID from T_INFO_PLANT where T_ORGID = '" + id + @"')
                        )) t1 where t1.T_ORGID = t.T_ORGID and t1.T_TIME > t.T_TIME)";


            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            rl = Convert.ToDouble(obj);
            double ljfzd = GetSumIrradiance(id, st, et);
            if (rl != 0 && ljfzd != 0)
                dlVal = GetStationDL(id, st, et) * 1000 / (rl * ljfzd);

            return Convert.ToDouble(Convert.ToDouble(dlVal).ToString("f2"));
        }
        #endregion

        #region 日发电效率
        public object GetDayEfficiency(string id, string time)
        {
            strSql = "select D_VALUE from T_INFO_STATISTICS where T_TJID='DPR' and T_TIME ='" + time + "' and T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            //if (obj != null && obj != DBNull.Value)
            //    dlVal = Convert.ToDouble(obj);
            return GetDouble(Convert.ToDouble(obj), 2);
        }
        #endregion

        #region 月发电效率
        public object GetMonthEfficiency(string id, string time)
        {
            strSql = "select D_VALUE from T_INFO_STATISTICS where T_TJID='MPR' and T_TIME ='" + time + "' and T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            //if (obj != null && obj != DBNull.Value)
            //    dlVal = Convert.ToDouble(obj);
            return GetDouble(Convert.ToDouble(obj), 2);
        }
        #endregion

        #region 年发电效率
        public double GetYearEfficiency(string id, string time)
        {
            strSql = "select D_VALUE from T_INFO_STATISTICS where T_TJID='YPR' and T_TIME ='" + time + "' and T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            if (obj != null && obj != DBNull.Value)
                dlVal = Convert.ToDouble(obj);
            else
                dlVal = 0;
            return GetDouble(Convert.ToDouble(dlVal), 2);
        }
        #endregion

        #region 单位容量发电量
        public double GetUnitDL(string id, string st, string et)
        {
            /*需要判断该段时间内是否增加容量  如果增加需要加权平均*/
            double rl = 0;
            strSql = " select top 1 D_RL From T_INFO_RL where T_ORGID='" + id + "' and T_TIME <='" + et + "' order by T_TIME desc";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            rl = Convert.ToDouble(obj);
            if (rl != 0)
                dlVal = GetStationDL(id, st, et) / rl;
            return Convert.ToDouble(Convert.ToDouble(dlVal).ToString("f2"));
        }
        #endregion

        #region 组件面积
        public double GetAssemblyArea(string id, string time)
        {
            strSql = "select D_MJ,T_ORGID,T_TIME From T_INFO_MJ where T_ORGID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID='" + id + "') and T_TIME<='" + time + "' order  by T_TIME desc";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            double v = 0;
            if (dt != null && dt.Rows.Count > 0)
            {
                DataView dv = new DataView(dt);                           //虚拟视图吧，我这么认为
                DataTable dtOrg = dv.ToTable(true, "T_ORGID");
                for (int i = 0; i < dtOrg.Rows.Count; i++)
                {
                    DataRow[] dr = dt.Select("T_ORGID='" + dtOrg.Rows[i][0] + "'", "T_TIME desc");
                    if (dr.Length > 0)
                    {
                        v += GetDouble(Convert.ToDouble(dr[0][0]), 2);
                    }
                }
            }
            return v;
        }

        #endregion

        #region 系统效率
        public double GetSystemEfficiency(string id, string st, string et)
        {
            object val = null;
            double ljfzd = GetSumIrradiance(id, st, et);
            double area = GetAssemblyArea(id, et);
            double dl = GetStationDL(id, st, et);
            if (ljfzd != 0 && area != 0)
                val = dl / (ljfzd * area);
            return Convert.ToDouble(Convert.ToDouble(val).ToString("f2"));
        }
        #endregion

        #region 组件计算转换效率
        public double GetConversionEfficiency(string id, string st, string et)
        {
            double xl = GetSumEfficiency(id, st, et);
            double dl = GetStationDL(id, st, et);
            if (xl != 0)
                dlVal = dl / xl;
            return Convert.ToDouble(Convert.ToDouble(dlVal).ToString("f2"));
        }
        #endregion

        #region Task
        public DataTable GetTask(string orgid, string st, string et)
        {
            strSql = "select u.ID_KEY, u.T_USERID,u.T_TASK,u.T_PERFORMANCE,u.T_ASSESS,u.I_FLAG,u.D_POINT,u.T_TIME,T_ORGID,m.T_USERNAME from(select * from T_INFO_INTERVENTION where T_ORGID='" + orgid + "' and T_TIME between '" + st + "' and '" + et + "') u inner join T_SYS_MEMBERINFO m on u.T_USERID=m.T_USERID ";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }

        public DataTable GetTaskPage(string orgid, string st, string et, int num, int index)
        {
            strSql = "select u.ID_KEY,u.T_USERID,u.T_TASK,u.T_PERFORMANCE,u.T_ASSESS,u.I_FLAG,u.D_POINT,u.T_TIME,T_ORGID,m.T_USERNAME from(select top " + num + " *from T_INFO_INTERVENTION where T_ORGID='" + orgid + "' and T_TIME between '" + st + "' and '" + et + "' and ID_KEY not in(select top " + (num * (index - 1)) + " ID_KEY from T_INFO_INTERVENTION where T_ORGID='" + orgid + "' and T_TIME between '" + st + "' and '" + et + "'  order by T_TIME asc) order by T_TIME asc) u inner join T_SYS_MEMBERINFO m on u.T_USERID=m.T_USERID";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }
        #endregion

        #region Persons
        public DataTable GetPerson(string orgId)
        {
            strSql = "select ID_KEY,T_USERID,T_USERNAME  from T_SYS_MEMBERINFO where T_USERID in(select T_USERID from T_SYS_MEMBERORG where T_ORGID='" + orgId + "')";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }
        #endregion

        #region 某段时间之内电量（趋势）
        public double[] GetStationChartDl(string orgid, DateTime sTime, DateTime eTime, int second)
        {
            double[] vals = null;
            strSql = "select SUM(D_VALUE) VAL,T_TIME from T_INFO_STATISTICS where T_TJID='DL' and T_ORGID in(select T_DISTRICTID from ";
            strSql += " T_BASE_DISTRICT where T_ORGID='" + orgid + "')  and T_TIME between '" + sTime + "' and '" + eTime + "' group by T_TIME order by T_TIME asc";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            string strVal = "";
            while (sTime < eTime)
            {
                DataRow[] dr = null;
                dr = dt.Select("T_TIME<='" + sTime + "'", "T_TIME desc");
                if (dr.Length > 0)
                    strVal += dr[0][0].ToString() + ",";
                sTime = sTime.AddSeconds(second);
            }
            if (strVal != "")
            {
                vals = new double[strVal.Split(',').Length - 1];
                for (int i = 0; i < vals.Length; i++)
                {
                    vals[i] = GetDouble(Convert.ToDouble(strVal.Split(',')[i]), 2);
                }
            }
            strSql = "";
            return vals;
        }
        #endregion

        #region 设备预付费用
        public DataTable GetPrepaid(int pageCount, int pageIndex, string orgId, DateTime st, DateTime et)
        {
            strSql = "select top " + pageCount + " * from (select pmc.ID_KEY,pmc.T_ORGID,pmc.T_TIME,pmc.T_USERNAME,pmc.D_QUANTITY,pmc.T_MACHINEID,pmc.D_PRICE,";
            strSql += "pmc.T_WTDATE,pmc.D_WTPERIOD,pmc.T_CONTACTNAME,pmc.T_CONTACTPHONE,pmc.T_CONTACTEMAIL,pmc.T_COMPANYID,pmc.T_TYPEID,pmc.T_COMPANYDESC_EN,pmc.T_COMPANYDESC_CN,";
            strSql += "et.T_TYPEDESC_EN,et.T_TYPEDESC_CN from (select pm.ID_KEY,pm.T_ORGID,pm.T_TIME,pm.T_USERNAME,pm.D_QUANTITY,pm.T_MACHINEID,pm.D_PRICE,";
            strSql += "pm.T_WTDATE,pm.D_WTPERIOD,pm.T_CONTACTNAME,pm.T_CONTACTPHONE,pm.T_CONTACTEMAIL,pm.T_COMPANYID,pm.T_TYPEID,cp.T_COMPANYDESC_EN,cp.T_COMPANYDESC_CN from (";
            strSql += "select pq.ID_KEY,pq.T_ORGID,pq.T_TIME,pq.T_USERNAME,pq.D_QUANTITY,pq.T_MACHINEID,pq.D_PRICE,";
            strSql += "pq.T_WTDATE,pq.D_WTPERIOD,pq.T_CONTACTNAME,pq.T_CONTACTPHONE,pq.T_CONTACTEMAIL,ma.T_COMPANYID,ma.T_TYPEID";
            strSql += " From T_INFO_PREPAYMENTEQ pq inner join T_BASE_MACHINE ma on pq.T_MACHINEID=ma.T_MACHINEID ";
            strSql += " where pq.T_ORGID ='" + orgId + "' and pq.T_TIME between '" + st + "' and '" + et + "') pm inner join T_BASE_MACHINECOMPANY cp ";
            strSql += " on pm.T_COMPANYID=cp.T_COMPANYID) pmc  inner join T_BASE_EQUIPTYPE et on pmc.T_TYPEID = et.T_TYPEID ";
            strSql += " ) pmct where pmct.ID_KEY not in(select top " + (pageCount * (pageIndex - 1)) + " id_KEY from T_INFO_PREPAYMENTEQ where T_TIME between '" + st + "' and ";
            strSql += " '" + et + "' order by T_TIME desc) order by pmct.T_TIME desc";

            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }

        public int GetPrepaidPageCount(string orgId, DateTime st, DateTime et)
        {
            strSql = "select count(*) from T_INFO_PREPAYMENTEQ where T_TIME between '" + st + "' and '" + et + "' and T_ORGID='" + orgId + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            return Convert.ToInt32(obj);
        }
        #endregion

        #region 初始化资产
        public DataTable GetInitializationAssets(int pageCount, int pageIndex, string orgId, DateTime st, DateTime et)
        {
            strSql = "select top " + pageCount + " * from (select pmc.ID_KEY,pmc.T_ORGID,pmc.T_TIME,pmc.T_USERNAME,pmc.D_QUANTITY,pmc.T_MACHINEID,pmc.D_PRICE,";
            strSql += "pmc.T_WTDATE,pmc.D_WTPERIOD,pmc.T_CONTACTNAME,pmc.T_CONTACTPHONE,pmc.T_CONTACTEMAIL,pmc.T_COMPANYID,pmc.T_TYPEID,pmc.T_COMPANYDESC_EN,pmc.T_COMPANYDESC_CN,";
            strSql += "et.T_TYPEDESC_EN,et.T_TYPEDESC_CN from (select pm.ID_KEY,pm.T_ORGID,pm.T_TIME,pm.T_USERNAME,pm.D_QUANTITY,pm.T_MACHINEID,pm.D_PRICE,";
            strSql += "pm.T_WTDATE,pm.D_WTPERIOD,pm.T_CONTACTNAME,pm.T_CONTACTPHONE,pm.T_CONTACTEMAIL,pm.T_COMPANYID,pm.T_TYPEID,cp.T_COMPANYDESC_EN,cp.T_COMPANYDESC_CN from (";
            strSql += "select pq.ID_KEY,pq.T_ORGID,pq.T_TIME,pq.T_USERNAME,pq.D_QUANTITY,pq.T_MACHINEID,pq.D_PRICE,";
            strSql += "pq.T_WTDATE,pq.D_WTPERIOD,pq.T_CONTACTNAME,pq.T_CONTACTPHONE,pq.T_CONTACTEMAIL,ma.T_COMPANYID,ma.T_TYPEID";
            strSql += " From T_INFO_initialcapex pq inner join T_BASE_MACHINE ma on pq.T_MACHINEID=ma.T_MACHINEID ";
            strSql += " where pq.T_ORGID ='" + orgId + "' and pq.T_TIME between '" + st + "' and '" + et + "') pm inner join T_BASE_MACHINECOMPANY cp ";
            strSql += " on pm.T_COMPANYID=cp.T_COMPANYID) pmc  inner join T_BASE_EQUIPTYPE et on pmc.T_TYPEID = et.T_TYPEID ";
            strSql += " ) pmct where pmct.ID_KEY not in(select top " + (pageCount * (pageIndex - 1)) + " id_KEY from T_INFO_initialcapex where T_TIME between '" + st + "' and ";
            strSql += " '" + et + "' order by T_TIME desc) order by pmct.T_TIME desc";

            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }

        public int GetInitializationAssetsPageCount(string orgId, DateTime st, DateTime et)
        {
            strSql = "select count(*) from T_INFO_initialcapex where T_TIME between '" + st + "' and '" + et + "' and T_ORGID='" + orgId + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            return Convert.ToInt32(obj);
        }
        #endregion

        #region 设备更换
        public DataTable GetReplacementEquipment(int pageCount, int pageIndex, string orgId, DateTime st, DateTime et)
        {
            strSql = "select top " + pageCount + " * from (select pmc.ID_KEY,pmc.T_ORGID,pmc.T_TIME,pmc.T_USERNAME,pmc.D_QUANTITY,pmc.T_MACHINEID,pmc.D_PRICE,";
            strSql += "pmc.T_WTDATE,pmc.D_WTPERIOD,pmc.T_CONTACTNAME,pmc.T_CONTACTPHONE,pmc.T_CONTACTEMAIL,pmc.T_COMPANYID,pmc.T_TYPEID,pmc.T_COMPANYDESC_EN,pmc.T_COMPANYDESC_CN,";
            strSql += "et.T_TYPEDESC_EN,et.T_TYPEDESC_CN from (select pm.ID_KEY,pm.T_ORGID,pm.T_TIME,pm.T_USERNAME,pm.D_QUANTITY,pm.T_MACHINEID,pm.D_PRICE,";
            strSql += "pm.T_WTDATE,pm.D_WTPERIOD,pm.T_CONTACTNAME,pm.T_CONTACTPHONE,pm.T_CONTACTEMAIL,pm.T_COMPANYID,pm.T_TYPEID,cp.T_COMPANYDESC_EN,cp.T_COMPANYDESC_CN from (";
            strSql += "select pq.ID_KEY,pq.T_ORGID,pq.T_TIME,pq.T_USERNAME,pq.D_QUANTITY,pq.T_MACHINEID,pq.D_PRICE,";
            strSql += "pq.T_WTDATE,pq.D_WTPERIOD,pq.T_CONTACTNAME,pq.T_CONTACTPHONE,pq.T_CONTACTEMAIL,ma.T_COMPANYID,ma.T_TYPEID";
            strSql += " From T_INFO_equipmentreplace pq inner join T_BASE_MACHINE ma on pq.T_MACHINEID=ma.T_MACHINEID ";
            strSql += " where pq.T_ORGID ='" + orgId + "' and pq.T_TIME between '" + st + "' and '" + et + "') pm inner join T_BASE_MACHINECOMPANY cp ";
            strSql += " on pm.T_COMPANYID=cp.T_COMPANYID) pmc  inner join T_BASE_EQUIPTYPE et on pmc.T_TYPEID = et.T_TYPEID ";
            strSql += " ) pmct where pmct.ID_KEY not in(select top " + (pageCount * (pageIndex - 1)) + " id_KEY from T_INFO_equipmentreplace where T_TIME between '" + st + "' and ";
            strSql += " '" + et + "' order by T_TIME desc) order by pmct.T_TIME desc";

            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }

        public int GetReplacementEquipmentPageCount(string orgId, DateTime st, DateTime et)
        {
            strSql = "select count(*) from T_INFO_equipmentreplace where T_TIME between '" + st + "' and '" + et + "' and T_ORGID='" + orgId + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            return Convert.ToInt32(obj);
        }
        #endregion

        #region 设备预付款（采购）
        public DataTable GetAdoptUpfrontPayment(int pageCount, int pageIndex, string orgId, DateTime st, DateTime et)
        {
            strSql = "";
            strSql = "select top " + pageCount + "  s.id_KEY,s.D_QUANTITY,s.D_PAYMENTAMOUNT,s.T_PUCHASINGDATE,s.T_MACHINEID,s.T_DUEDATE,";
            strSql += "s.T_COMPANYID,s.T_TYPEID,s.T_TYPEDESC_CN,s.T_TYPEDESC_EN,s.T_COMPANYDESC_CN,s.T_COMPANYDESC_EN from ";
            strSql += "( select p.id_KEY, p.D_QUANTITY,p.D_PAYMENTAMOUNT,p.T_PUCHASINGDATE,p.T_DUEDATE,";
            strSql += " p.T_COMPANYID,p.T_TYPEID,p.T_TYPEDESC_CN,p.T_TYPEDESC_EN,p.T_MACHINEID,mc.T_COMPANYDESC_CN,mc.T_COMPANYDESC_EN from ";
            strSql += " (select d.ID_KEY, d.T_ORGID, d.D_QUANTITY,d.D_PAYMENTAMOUNT,d.T_PUCHASINGDATE,d.T_DUEDATE,";
            strSql += " d.T_COMPANYID,d.T_TYPEID,d.T_MACHINEID,c.T_TYPEDESC_CN,c.T_TYPEDESC_EN from ";
            strSql += " (select e.ID_KEY, e.T_ORGID, e.D_QUANTITY,e.D_PAYMENTAMOUNT,e.T_PUCHASINGDATE,e.T_MACHINEID,e.T_DUEDATE,";
            strSql += " m.T_COMPANYID,m.T_TYPEID from T_INFO_EQUIPMENTPROCUREMENT e";
            strSql += "  inner join T_BASE_MACHINE m on e.T_MACHINEID = m.T_MACHINEID) d ";
            strSql += "   inner join T_BASE_EQUIPTYPE c on d.T_TYPEID=c.T_TYPEID) p ";
            strSql += "  inner join T_BASE_MACHINECOMPANY mc on p.T_COMPANYID = mc.T_COMPANYID";
            strSql += " where   p.T_PUCHASINGDATE between '" + st + "' and '" + et + "'";
            strSql += " and  p.T_ORGID ='" + orgId + "') s where T_PUCHASINGDATE between '" + st + "' and '" + et + "' and id_key not in(";
            strSql += "select top " + (pageCount * (pageIndex - 1)) + " ID_KEY from T_INFO_EQUIPMENTPROCUREMENT where ";
            strSql += " T_PUCHASINGDATE between '" + st + "' and '" + et + "' and T_ORGID='" + orgId + "'";
            strSql += " order by T_PUCHASINGDATE desc) order by s.T_PUCHASINGDATE desc";

            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }

        public int GetAdoptUpfrontPaymentPageCount(string orgId, DateTime st, DateTime et)
        {
            strSql = "select count(*) from T_INFO_EQUIPMENTPROCUREMENT where T_PUCHASINGDATE between'" + st + "' and '" + et + "' and T_ORGID='" + orgId + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            return Convert.ToInt32(obj);
        }

        #endregion

        #region 工资福利
        public DataTable GetWagesBenefits(int pageCount, int pageIndex, string orgId, DateTime st, DateTime et)
        {
            strSql = "";
            strSql += "select top " + pageCount + " row_number() over(order by T_DUEDATE asc)as ";
            strSql += "ID, ID_KEY,T_TYPEID,D_PAYMENTAMOUNT,T_DUEDATE,T_TYPEDESC_EN,T_TYPEDESC_CN from ";
            strSql += "(select  s.ID_key,s.T_TYPEID,s.D_PAYMENTAMOUNT,s.T_DUEDATE,m.T_TYPEDESC_EN,m.T_TYPEDESC_CN ";
            strSql += "from T_INFO_SALARYWELFARE s inner join T_BASE_SALARYTYPE m on ";
            strSql += "S.T_TYPEID=m.T_TYPEID where s.T_DUEDATE between '" + st + "'  and '" + et + "' and T_ORGID='" + orgId + "')";
            strSql += " mq where ID_KEY not in (select top " + (pageCount * (pageIndex - 1));
            strSql += " ID_KEY from T_INFO_SALARYWELFARE where T_ORGID='" + orgId + "' and T_DUEDATE between";
            strSql += " '" + st + "'  and '" + et + "'  order by T_DUEDATE desc)  order by T_DUEDATE desc";

            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }

        public double GetWagesBenefitsSum(string id, DateTime st, DateTime et)
        {
            strSql = "";
            strSql = "select SUM(D_PAYMENTAMOUNT) value from T_INFO_SALARYWELFARE where T_ORGID='" + id + "' and T_DUEDATE between '" + st + "' and '" + et + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            if (obj != null)
                return Convert.ToDouble(obj);
            else
                return 0;
        }

        public int GetWagesBenefitsPageCount(string orgId, DateTime st, DateTime et)
        {
            strSql = "select count(*) from T_INFO_SALARYWELFARE where T_DUEDATE between'" + st + "' and '" + et + "' and T_ORGID='" + orgId + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            return Convert.ToInt32(obj);
        }
        #endregion

        #region 运维费用
        public DataTable GetOperation(int pageCount, int pageIndex, string orgId, DateTime st, DateTime et)
        {
            strSql = "";
            strSql += "select top " + pageCount + " row_number() over(order by T_TIME asc)as ";
            strSql += "ID,ID_KEY,T_TYPEID,D_PAYMENTAMOUNT,T_TIME,B_ATTACHMENT,T_ATTACHMENTNAME,T_TYPEDESC_EN,T_TYPEDESC_CN from ";
            strSql += "(select o.ID_KEY,o.T_TYPEID,o.D_PAYMENTAMOUNT,o.T_TIME,o.B_ATTACHMENT,O.T_ATTACHMENTNAME,t.T_TYPEDESC_EN,t.T_TYPEDESC_CN from T_INFO_OMCOST o inner join ";
            strSql += "T_BASE_OMCOSTTYPE t on o.T_TYPEID = t.T_TYPEID where o.T_TIME between '" + st + "' and '" + et + "' and o.T_ORGID='" + orgId + "') om where ID_KEY not in";
            strSql += "(select top " + (pageCount * (pageIndex - 1)) + " ID_KEY from T_INFO_OMCOST where T_TIME between '" + st + "' and '" + et + "' and T_ORGID='" + orgId + "'";
            strSql += "order by T_TIME desc) order by T_TIME desc";

            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }
        //

        public double GetOperationSum(string id, DateTime st, DateTime et)
        {
            strSql = "";
            strSql = "select SUM(D_PAYMENTAMOUNT) value from T_INFO_OMCOST where T_ORGID='" + id + "' and T_TIME between '" + st + "' and '" + et + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            if (obj != null)
                return Convert.ToDouble(obj);
            else
                return 0;
        }

        public int GetOperationPageCount(string orgId, DateTime st, DateTime et)
        {
            strSql = "select count(*) from T_INFO_OMCOST where T_TIME between'" + st + "' and '" + et + "' and T_ORGID='" + orgId + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            return Convert.ToInt32(obj);
        }

        public DataTable GetMonthOperation(string id, DateTime st, DateTime et)
        {
            strSql = @"select row_number() over(order by NDATE asc)as ID, SUM(D_PAYMENTAMOUNT) VAL,base.NDATE,TTYPE as TTYPE, CN, EN from(   
			  select o.T_ORGID,o.T_TYPEID TTYPE,o.D_PAYMENTAMOUNT,convert(varchar(7),T_TIME,111) NDAtE,d.T_TYPEDESC_CN CN,d.T_TYPEDESC_EN EN 
              from T_INFO_OMCOST  o inner join T_BASE_OMCOSTTYPE d on o.T_TYPEID=d.T_TYPEID WHERE T_TIME BETWEEN '" + st + "' and '" + et + "') base where T_ORGID='" + id + "'";
            strSql += "    group by TTYPE,NDATE,EN,CN order by NDATE desc";

            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }

        #endregion

        #region 区域ID获取
        public DataTable GetAreaId(string orgid)
        {
            strSql = "";
            strSql += "select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID='" + orgid + "'";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }
        #endregion

        #region 国家
        public object GetCountry(string id)
        {
            strSql = "select T_NATIONID from  T_INFO_PLANT where  T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });

            return obj;
        }
        #endregion

        #region 质保期
        public DataTable GetGuaranteePeriod(string id)
        {
            strSql = "";
            //            strSql = @"select cx.T_ORGID,cx.T_TIME,cx.T_USERNAME,cx.D_QUANTITY,cx.T_MACHINEID,cx.D_PRICE,cx.D_WTPERIOD,cx.T_WTDATE,
            //cx.T_CONTACTNAME,cx.T_CONTACTPHONE,cx.T_CONTACTEMAIL,cx.T_ORGDESC_CN,cx.T_ORGDESC_EN,cx.EN,cx.CN,ci.T_COMPANYID,ci.T_COMPANYDESC_CN,ci.T_COMPANYDESC_EN,
            //ci.T_TYPEDESC_CN,ci.T_TYPEDESC_EN from(select x.T_ORGID,x.T_TIME,x.T_USERNAME,x.D_QUANTITY,x.T_MACHINEID,x.D_PRICE,x.D_WTPERIOD,x.T_WTDATE,
            //x.T_CONTACTNAME,x.T_CONTACTPHONE,x.T_CONTACTEMAIL,o.T_ORGDESC_CN,o.T_ORGDESC_EN,'initialcapex' EN ,'初始固定资产' CN
            //from T_INFO_initialcapex x inner join T_BASE_ORG o on x.T_ORGID = o.T_ORGID where x.T_ORGID='" + id + "' and  DATEADD(MM,-3,x.T_WTDATE)<=GETDATE() and x.T_WTDATE>=GETDATE()) cx left join (select cleft.T_COMPANYID,cleft.T_MACHINEID,cleft.T_TYPEDESC_CN,cleft.T_TYPEDESC_EN,my.T_COMPANYDESC_CN,my.T_COMPANYDESC_EN  From(select me.T_COMPANYID,me.T_MACHINEID,ee.T_TYPEDESC_CN,ee.T_TYPEDESC_EN From T_BASE_MACHINE me left join T_BASE_EQUIPTYPE ee on me.T_TYPEID=ee.T_TYPEID) c";
            //            strSql += @"left join T_BASE_MACHINECOMPANY my on cleft.T_COMPANYID=my.T_COMPANYID) ci on cx.T_MACHINEID=ci.T_MACHINEID union all select cx.T_ORGID,cx.T_TIME,cx.T_USERNAME,cx.D_QUANTITY,cx.T_MACHINEID,cx.D_PRICE,cx.D_WTPERIOD,cx.T_WTDATE,
            //cx.T_CONTACTNAME,cx.T_CONTACTPHONE,cx.T_CONTACTEMAIL,cx.T_ORGDESC_CN,cx.T_ORGDESC_EN,cx.EN,cx.CN,ci.T_COMPANYID,ci.T_COMPANYDESC_CN,ci.T_COMPANYDESC_EN,
            //ci.T_TYPEDESC_CN,ci.T_TYPEDESC_EN from(select x.T_ORGID,x.T_TIME,x.T_USERNAME,x.D_QUANTITY,x.T_MACHINEID,x.D_PRICE,x.D_WTPERIOD,x.T_WTDATE,
            //x.T_CONTACTNAME,x.T_CONTACTPHONE,x.T_CONTACTEMAIL,o.T_ORGDESC_CN,o.T_ORGDESC_EN,'equipmentreplace' EN ,'设备更换' CN from T_INFO_equipmentreplace x inner join T_BASE_ORG o on x.T_ORGID = o.T_ORGID where x.T_ORGID='" + id + "' and  ";
            //            strSql += @"DATEADD(MM,-3,x.T_WTDATE)<=GETDATE() and x.T_WTDATE>=GETDATE()) cx left join (select c.T_COMPANYID,c.T_MACHINEID,c.T_TYPEDESC_CN,c.T_TYPEDESC_EN,my.T_COMPANYDESC_CN,my.T_COMPANYDESC_EN  From(
            //select me.T_COMPANYID,me.T_MACHINEID,ee.T_TYPEDESC_CN,ee.T_TYPEDESC_EN From T_BASE_MACHINE me left join T_BASE_EQUIPTYPE ee on me.T_TYPEID=ee.T_TYPEID) c
            //left join T_BASE_MACHINECOMPANY my on c.T_COMPANYID=my.T_COMPANYID) ci on cx.T_MACHINEID=ci.T_MACHINEID union all select cx.T_ORGID,cx.T_TIME,cx.T_USERNAME,cx.D_QUANTITY,cx.T_MACHINEID,cx.D_PRICE,cx.D_WTPERIOD,cx.T_WTDATE,
            //cx.T_CONTACTNAME,cx.T_CONTACTPHONE,cx.T_CONTACTEMAIL,cx.T_ORGDESC_CN,cx.T_ORGDESC_EN,cx.EN,cx.CN,ci.T_COMPANYID,ci.T_COMPANYDESC_CN,ci.T_COMPANYDESC_EN,
            //ci.T_TYPEDESC_CN,ci.T_TYPEDESC_EN from(select x.T_ORGID,x.T_TIME,x.T_USERNAME,x.D_QUANTITY,x.T_MACHINEID,x.D_PRICE,x.D_WTPERIOD,x.T_WTDATE,
            //x.T_CONTACTNAME,x.T_CONTACTPHONE,x.T_CONTACTEMAIL,o.T_ORGDESC_CN,o.T_ORGDESC_EN,'instock' EN ,'库存' CN
            //from T_INFO_INSTOCK x inner join T_BASE_ORG o on x.T_ORGID = o.T_ORGID  where x.T_ORGID='" + id + "' and  ";
            //            strSql += @"DATEADD(MM,-3,x.T_WTDATE)<=GETDATE() and x.T_WTDATE>=GETDATE()) cx left join (select c.T_COMPANYID,c.T_MACHINEID,c.T_TYPEDESC_CN,c.T_TYPEDESC_EN,my.T_COMPANYDESC_CN,my.T_COMPANYDESC_EN  From(
            //select me.T_COMPANYID,me.T_MACHINEID,ee.T_TYPEDESC_CN,ee.T_TYPEDESC_EN From T_BASE_MACHINE me left join T_BASE_EQUIPTYPE ee on me.T_TYPEID=ee.T_TYPEID) c
            //left join T_BASE_MACHINECOMPANY my on c.T_COMPANYID=my.T_COMPANYID) ci on cx.T_MACHINEID=ci.T_MACHINEID";

            //由于组织机构修改，查询内容变更
            strSql = @"select cx.T_ORGID,cx.T_TIME,cx.T_USERNAME,cx.D_QUANTITY,cx.T_MACHINEID,cx.D_PRICE,cx.D_WTPERIOD,cx.T_WTDATE,
cx.T_CONTACTNAME,cx.T_CONTACTPHONE,cx.T_CONTACTEMAIL,cx.T_PLANTDESC,cx.EN,cx.CN,ci.T_COMPANYID,ci.T_COMPANYDESC_CN,ci.T_COMPANYDESC_EN,
ci.T_TYPEDESC_CN,ci.T_TYPEDESC_EN from(select x.T_ORGID,x.T_TIME,x.T_USERNAME,x.D_QUANTITY,x.T_MACHINEID,x.D_PRICE,x.D_WTPERIOD,x.T_WTDATE,
x.T_CONTACTNAME,x.T_CONTACTPHONE,x.T_CONTACTEMAIL,o.T_PLANTDESC,'initialcapex' EN ,'初始固定资产' CN
from T_INFO_initialcapex x inner join T_INFO_PLANT o on x.T_ORGID = o.T_ORGID where x.T_ORGID='" + id + "' and  DATEADD(MM,-3,x.T_WTDATE)<=GETDATE() and x.T_WTDATE>=GETDATE()) cx left join (select cleft.T_COMPANYID,cleft.T_MACHINEID,cleft.T_TYPEDESC_CN,cleft.T_TYPEDESC_EN,my.T_COMPANYDESC_CN,my.T_COMPANYDESC_EN  From(select me.T_COMPANYID,me.T_MACHINEID,ee.T_TYPEDESC_CN,ee.T_TYPEDESC_EN From T_BASE_MACHINE me left join T_BASE_EQUIPTYPE ee on me.T_TYPEID=ee.T_TYPEID) c";
            strSql += @"left join T_BASE_MACHINECOMPANY my on cleft.T_COMPANYID=my.T_COMPANYID) ci on cx.T_MACHINEID=ci.T_MACHINEID union all select cx.T_ORGID,cx.T_TIME,cx.T_USERNAME,cx.D_QUANTITY,cx.T_MACHINEID,cx.D_PRICE,cx.D_WTPERIOD,cx.T_WTDATE,
cx.T_CONTACTNAME,cx.T_CONTACTPHONE,cx.T_CONTACTEMAIL,cx.T_PLANTDESC,cx.EN,cx.CN,ci.T_COMPANYID,ci.T_COMPANYDESC_CN,ci.T_COMPANYDESC_EN,
ci.T_TYPEDESC_CN,ci.T_TYPEDESC_EN from(select x.T_ORGID,x.T_TIME,x.T_USERNAME,x.D_QUANTITY,x.T_MACHINEID,x.D_PRICE,x.D_WTPERIOD,x.T_WTDATE,
x.T_CONTACTNAME,x.T_CONTACTPHONE,x.T_CONTACTEMAIL,o.T_PLANTDESC,'equipmentreplace' EN ,'设备更换' CN from T_INFO_equipmentreplace x inner join T_INFO_PLANT o on x.T_ORGID = o.T_ORGID where x.T_ORGID='" + id + "' and  ";
            strSql += @"DATEADD(MM,-3,x.T_WTDATE)<=GETDATE() and x.T_WTDATE>=GETDATE()) cx left join (select c.T_COMPANYID,c.T_MACHINEID,c.T_TYPEDESC_CN,c.T_TYPEDESC_EN,my.T_COMPANYDESC_CN,my.T_COMPANYDESC_EN  From(
select me.T_COMPANYID,me.T_MACHINEID,ee.T_TYPEDESC_CN,ee.T_TYPEDESC_EN From T_BASE_MACHINE me left join T_BASE_EQUIPTYPE ee on me.T_TYPEID=ee.T_TYPEID) c
left join T_BASE_MACHINECOMPANY my on c.T_COMPANYID=my.T_COMPANYID) ci on cx.T_MACHINEID=ci.T_MACHINEID union all select cx.T_ORGID,cx.T_TIME,cx.T_USERNAME,cx.D_QUANTITY,cx.T_MACHINEID,cx.D_PRICE,cx.D_WTPERIOD,cx.T_WTDATE,
cx.T_CONTACTNAME,cx.T_CONTACTPHONE,cx.T_CONTACTEMAIL,cx.T_PLANTDESC,cx.EN,cx.CN,ci.T_COMPANYID,ci.T_COMPANYDESC_CN,ci.T_COMPANYDESC_EN,
ci.T_TYPEDESC_CN,ci.T_TYPEDESC_EN from(select x.T_ORGID,x.T_TIME,x.T_USERNAME,x.D_QUANTITY,x.T_MACHINEID,x.D_PRICE,x.D_WTPERIOD,x.T_WTDATE,
x.T_CONTACTNAME,x.T_CONTACTPHONE,x.T_CONTACTEMAIL,o.T_PLANTDESC,'instock' EN ,'库存' CN
from T_INFO_INSTOCK x inner join T_INFO_PLANT o on x.T_ORGID = o.T_ORGID  where x.T_ORGID='" + id + "' and  ";
            strSql += @"DATEADD(MM,-3,x.T_WTDATE)<=GETDATE() and x.T_WTDATE>=GETDATE()) cx left join (select c.T_COMPANYID,c.T_MACHINEID,c.T_TYPEDESC_CN,c.T_TYPEDESC_EN,my.T_COMPANYDESC_CN,my.T_COMPANYDESC_EN  From(
select me.T_COMPANYID,me.T_MACHINEID,ee.T_TYPEDESC_CN,ee.T_TYPEDESC_EN From T_BASE_MACHINE me left join T_BASE_EQUIPTYPE ee on me.T_TYPEID=ee.T_TYPEID) c
left join T_BASE_MACHINECOMPANY my on c.T_COMPANYID=my.T_COMPANYID) ci on cx.T_MACHINEID=ci.T_MACHINEID";

            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }
        #endregion

        #region 四舍五入
        /// <summary>
        /// 四舍五入
        /// </summary>
        /// <param name="result">要转换的数值</param>
        /// <param name="num">保留位数</param>
        /// <returns></returns>
        public double GetDouble(double result, int num)
        {
            if (!double.IsNaN(result))
            {
                string res = result.ToString();
                string results = "";
                int index = res.IndexOf('.');

                if (res.Length - index == num + 1)
                    return Convert.ToDouble(res);
                else
                {
                    if (index > 0)
                    {
                        index += num;
                        res = res + "000000000000000000";
                        res = res.Remove(0, index + 1);
                        results = result + "000000000000000000";
                        results = results.ToString().Substring(0, index + 1);
                        res = res.Substring(0, 1);

                        string point = "0.";

                        for (int count = 0; count < num - 1; count++)
                        {
                            point += "0";
                        }
                        point += "1";


                        if (Convert.ToInt32(res) > 4)
                        {
                            results = (Convert.ToDouble(results) + Convert.ToDouble(point)).ToString();
                            res = results;
                        }
                        else
                        {
                            res = results;
                        }
                    }
                    else
                    {
                        res += ".";
                        for (int i = 0; i < num; i++)
                        {
                            res += "0";
                        }
                    }
                    return Convert.ToDouble(res);
                }
            }
            else
                return result;
        }
        #endregion

        #region 大类故障详细信息 逆变器
        public DataTable GetFaultRecord(string id, string type, DateTime sTime, DateTime eTime)
        {
            strSql = @"select *From T_INFO_FAULTRECORD where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = '" + id + "')) and ";
            strSql += "(ID_KEY not in (select ID_KEY from T_INFO_FAULTRECORD where T_ENDTIME <'" + sTime + "' or T_STARTTIME>'" + eTime + "'))and T_Watchid='" + type + "'";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }
        #endregion

        #region 大类故障次数 逆变器
        public DataTable GetFaultRecordCount(string id, DateTime sTime, DateTime eTime)
        {
            if (id.IndexOf("'") > -1)
            {
                //                strSql = @"select distinct isnull(ft.Num,0) Num,e.T_TYPEID,ft.T_ORGID,ft.T_UNITDESC_CN,ft.T_UNITDESC_EN,ft.T_MACHINEID,e.T_TYPEDESC From (select  f.Num,f.T_WATCHID,f.T_ORGID,b.T_UNITDESC_CN,b.T_UNITDESC_EN,b.T_MACHINEID from (
                //select COUNT(*) Num,T_WATCHID,T_ORGID From T_INFO_FAULTRECORD where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in(" + id + ")) and T_MACHINEID in(select T_MACHINEID from T_BASE_MACHINE where T_TYPEID='nbq')) and ";
                //                strSql += @"T_WATCHID in(select distinct T_TYPEID from T_BASE_FAULTPARA where T_MACHINEID in(select T_MACHINEID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in(" + id + ")))) and";
                //                strSql += @" ID_KEY not in (select ID_KEY from T_INFO_FAULTRECORD where T_STARTTIME between '" + sTime + "' and '" + eTime + "' or T_ENDTIME is null) group by T_WATCHID,T_ORGID) f left join T_BASE_UNIT b on f.T_ORGID=b.T_UNITID) ft left join T_BASE_FAULTPARA fa on ft.T_WATCHID=fa.T_TYPEID  left join T_BASE_FAULTTYPE e on fa.T_TYPEID = e.T_TYPEID";
                strSql = @"select distinct isnull(ft.Num,0) Num,e.T_TYPEID,ft.T_ORGID,ft.T_UNITDESC_CN,ft.T_UNITDESC_EN,ft.T_MACHINEID,e.T_TYPEDESC From (
select  f.Num,f.T_WATCHID,f.T_ORGID,b.T_UNITDESC_CN,b.T_UNITDESC_EN,b.T_MACHINEID from (
select COUNT(*) Num,T_WATCHID,T_ORGID From T_INFO_FAULTRECORD where T_ORGID in(
select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = 'WX1#ST'))
   and T_WATCHID in(select distinct T_TYPEID from T_BASE_FAULTPARA where T_MACHINEID in(
   select T_MACHINEID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in(" + id + @"))))
    and T_STARTTIME between '" + sTime + @"' and '" + eTime + @"' or T_ENDTIME is null

    group by T_WATCHID,T_ORGID) f left join T_BASE_UNIT b on f.T_ORGID = b.T_UNITID) ft left join T_BASE_FAULTPARA fa on

    ft.T_WATCHID = fa.T_TYPEID  left join T_BASE_FAULTTYPE e on fa.T_TYPEID = e.T_TYPEID";
            }
            else
            {
                //                strSql = @"select distinct isnull(ft.Num,0) Num,e.T_TYPEID,ft.T_ORGID,ft.T_UNITDESC_CN,ft.T_UNITDESC_EN,ft.T_MACHINEID,e.T_TYPEDESC From (select  f.Num,f.T_WATCHID,f.T_ORGID,b.T_UNITDESC_CN,b.T_UNITDESC_EN,b.T_MACHINEID from (
                //select COUNT(*) Num,T_WATCHID,T_ORGID From T_INFO_FAULTRECORD where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = '" + id + "')  and T_MACHINEID in(select T_MACHINEID from T_BASE_MACHINE where T_TYPEID='nbq')) and ";
                //                strSql += @"T_WATCHID in(select distinct T_TYPEID from T_BASE_FAULTPARA where T_MACHINEID in(select T_MACHINEID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = '" + id + "'))) and";
                //                strSql += @" ID_KEY not in (select ID_KEY from T_INFO_FAULTRECORD where T_STARTTIME between '" + sTime + "' and '" + eTime + "' or T_ENDTIME is null) group by T_WATCHID,T_ORGID) f left join T_BASE_UNIT b on f.T_ORGID=b.T_UNITID) ft left join T_BASE_FAULTPARA fa on ft.T_WATCHID=fa.T_TYPEID  left join T_BASE_FAULTTYPE e on fa.T_TYPEID = e.T_TYPEID";
                strSql = @"select distinct isnull(ft.Num,0) Num,e.T_TYPEID,ft.T_ORGID,ft.T_UNITDESC_CN,ft.T_UNITDESC_EN,ft.T_MACHINEID,e.T_TYPEDESC From (
select  f.Num,f.T_WATCHID,f.T_ORGID,b.T_UNITDESC_CN,b.T_UNITDESC_EN,b.T_MACHINEID from (
select COUNT(*) Num,T_WATCHID,T_ORGID From T_INFO_FAULTRECORD where T_ORGID in(
select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = 'WX1#ST'))
   and T_WATCHID in(select distinct T_TYPEID from T_BASE_FAULTPARA where T_MACHINEID in(
   select T_MACHINEID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = '" + id + @"')))
    and  T_STARTTIME between '" + sTime + @"' and '" + eTime + @"' or T_ENDTIME is null 
	group by T_WATCHID,T_ORGID) f left join T_BASE_UNIT b on f.T_ORGID=b.T_UNITID) ft left join T_BASE_FAULTPARA fa on 
	ft.T_WATCHID=fa.T_TYPEID  left join T_BASE_FAULTTYPE e on fa.T_TYPEID = e.T_TYPEID";
            }
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }
        #endregion

        #region 大类故障次数 逆变器
        public DataTable GetFaultRecordCount(string ids)
        {

            strSql = @"select distinct isnull(ft.Num,0) Num,ft.T_WATCHID,ft.T_ORGID,ft.T_UNITDESC_CN,ft.T_UNITDESC_EN,FT.T_PLANTID,ft.T_MACHINEID From (select  f.Num,f.T_WATCHID,f.T_ORGID,b.T_UNITDESC_CN,b.T_UNITDESC_EN,b.T_MACHINEID,c.T_ORGID AS T_PLANTID from (select COUNT(*) Num,T_WATCHID,T_ORGID From T_INFO_FAULTRECORD where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in(" + ids + "))) and ";
            strSql += @"T_WATCHID in(select distinct T_TYPEID from T_BASE_FAULTPARA where T_MACHINEID in(select T_MACHINEID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in(" + ids + ")))) and";
            strSql += @" ( T_ENDTIME is null ) group by T_WATCHID,T_ORGID) f left join T_BASE_UNIT b on f.T_ORGID=b.T_UNITID LEFT JOIN dbo.T_BASE_DISTRICT C ON b.T_DISTRICTID=c.T_DISTRICTID) ft right join T_BASE_FAULTPARA fa on ft.T_WATCHID=fa.T_TYPEID";

            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }
        #endregion

        #region 小类故障次数 逆变器
        public DataTable GetFaultDetailCount(string ids)
        {
            if (ids.IndexOf("'") > 0)
            {
                strSql = @"select distinct isnull(ft.Num,0) Num,ft.T_WATCHID,ft.T_ORGID,ft.T_UNITDESC_CN,ft.T_UNITDESC_EN,ft.T_MACHINEID,ft.T_PLANTID,fa.T_FAULTDESC_EN,fa.T_FAULTDESC_CN From (select  f.Num,f.T_WATCHID,f.T_ORGID,b.T_UNITDESC_CN,b.T_UNITDESC_EN,b.T_MACHINEID,c.T_ORGID AS T_PLANTID from (
select COUNT(*) Num,T_WATCHID,T_ORGID From T_INFO_FAULTDETAIL where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in(" + ids + "))) and ";
                strSql += @"T_WATCHID in(select distinct T_FAULTID from T_BASE_FAULTPARA where T_MACHINEID in(select T_MACHINEID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in (" + ids + ")))) and";
                strSql += @" ( T_ENDTIME is null ) group by T_WATCHID,T_ORGID) f left join T_BASE_UNIT b on f.T_ORGID=b.T_UNITID LEFT JOIN T_BASE_DISTRICT c ON b.T_DISTRICTID=c.T_DISTRICTID) ft right join T_BASE_FAULTPARA fa on ft.T_WATCHID=fa.T_FAULTID";
            }
            else
            {
                strSql = @"select distinct isnull(ft.Num,0) Num,ft.T_WATCHID,ft.T_ORGID,ft.T_UNITDESC_CN,ft.T_UNITDESC_EN,ft.T_MACHINEID,ft.T_PLANTID,fa.T_FAULTDESC_EN,fa.T_FAULTDESC_CN From (select  f.Num,f.T_WATCHID,f.T_ORGID,b.T_UNITDESC_CN,b.T_UNITDESC_EN,b.T_MACHINEID,c.T_ORGID AS T_PLANTID from (select COUNT(*) Num,T_WATCHID,T_ORGID From T_INFO_FAULTDETAIL where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID ='" + ids + "')) and ";
                strSql += @"T_WATCHID in(select distinct T_FAULTID from T_BASE_FAULTPARA where T_MACHINEID in(select T_MACHINEID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID ='" + ids + "'))) and";
                strSql += @" ( T_ENDTIME is null ) group by T_WATCHID,T_ORGID) f left join T_BASE_UNIT b on f.T_ORGID=b.T_UNITID LEFT JOIN T_BASE_DISTRICT c ON b.T_DISTRICTID=c.T_DISTRICTID) ft right join T_BASE_FAULTPARA fa on ft.T_WATCHID=fa.T_FAULTID";

            }
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }
        #endregion

        #region 小类故障明细 逆变器
        public DataTable GetFaultDetail(string id, string type, DateTime sTime, DateTime eTime)
        {
            strSql = string.Format("select distinct f.ID_KEY, f.T_WATCHID, f.T_STARTTIME, f.T_ENDTIME, f.T_ORGID, p.T_FAULTDESC_EN, p.T_FAULTDESC_CN from(select ID_KEY, T_WATCHID, T_STARTTIME, T_ENDTIME, T_ORGID From T_INFO_FAULTDETAIL where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = '{0}')) and (ID_KEY not in (select ID_KEY from T_INFO_FAULTRECORD where T_ENDTIME <'{1}' or T_STARTTIME>'{2}'))and T_Watchid in (select T_FAULTID from T_BASE_FAULTPARA where T_TYPEID = '{3}')) f left join T_BASE_FAULTPARA p on f.T_WATCHID = p.T_FAULTID", id, sTime, eTime, type);
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }

        /*小类故障明细---->正在发生故障*/
        public DataTable GetFaultDetaiInfo(string stationId)
        {
            if (stationId.IndexOf("'") > -1)
            {
                strSql = "select bt.T_TYPEID,F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,bt.T_TYPEDESC,u.T_UNITDESC_CN,d.T_ORGID stationID from(select T_WATCHID,T_STARTTIME,T_ENDTIME,A.T_ORGID,C.T_FAULTDESC_CN,C.T_FAULTDESC_EN,C.T_TYPEID From T_INFO_FAULTDETAIL A LEFT JOIN dbo.T_BASE_UNIT B ON A.T_ORGID=B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID=C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in(" + stationId + ")))  and T_ENDTIME is null) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID  left join T_BASE_DISTRICT d on u.T_DISTRICTID = d.T_DISTRICTID";
            }
            else
            {
                strSql = "select bt.T_TYPEID,F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,bt.T_TYPEDESC,u.T_UNITDESC_CN,d.T_ORGID stationID from(select  T_WATCHID,T_STARTTIME,T_ENDTIME,A.T_ORGID,C.T_FAULTDESC_CN,C.T_FAULTDESC_EN,C.T_TYPEID From T_INFO_FAULTDETAIL A LEFT JOIN dbo.T_BASE_UNIT B ON A.T_ORGID=B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID=C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID ='" + stationId + "'))  and T_ENDTIME is null) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID  left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID  left join T_BASE_DISTRICT d on u.T_DISTRICTID = d.T_DISTRICTID";

            }
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }

        /*正在发生的故障*/
        public DataTable GetFault()
        {
            strSql = @"select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID,b.T_UNITDESC_CN, tp.T_PLANTDESC,CT.T_CITYDESC,
									    C.T_FAULTDESC_CN, C.T_FAULTDESC_EN,C.T_TYPEID,C.T_FAULTID,f.T_TYPEDESC From 
										T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C 
										ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID left join T_BASE_DISTRICT td on b.T_DISTRICTID=td.T_DISTRICTID 
										left join T_INFO_PLANT tp on td.T_ORGID = tp.T_ORGID left join T_BASE_CITY CT on tp.T_CITYID= ct.T_CITYID left join T_BASE_FAULTTYPE f on c.T_TYPEID = f.T_TYPEID where 								
										 T_ENDTIME is null order by T_CITYDESC,T_PLANTDESC,T_UNITDESC_CN,T_TYPEID asc";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }

        /*统计所有电站小类故障数量*/
        public DataTable GetFaultDetaiCount()
        {
            strSql = "select count(*) val,s.T_ORGID from T_INFO_FAULTDETAIL f inner join (select u.T_UNITID, d.T_ORGID from T_BASE_UNIT u left join T_BASE_DISTRICT d on u.T_DISTRICTID = d.T_DISTRICTID) s on f.T_ORGID = s.T_UNITID where T_ENDTIME is null group by s.T_ORGID";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }

        /// <summary>
        /// 小类故障查询
        /// </summary>
        /// <param name="stationId">产站编码</param>
        /// <param name="sTime"></param>
        /// <param name="eTime"></param>
        /// <param name="title">故障名称--->模糊查询</param>
        /// <returns></returns>
        public DataTable GetFaultDetailInfo(string stationId, DateTime sTime, DateTime eTime, string title, string faultType, int count, int pageIndex)
        {
            if (title != "")
            {
                if (faultType != "")
                {
                    if (stationId.IndexOf("'") > -1)
                        //strSql = string.Format(@"select top {0} F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_PLANTDESC,F.T_CITYDESC,u.T_UNITDESC_CN,bt.T_TYPEDESC,F.T_FAULTID from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, tp.T_PLANTDESC,CT.T_CITYDESC,C.T_FAULTDESC_CN, C.T_FAULTDESC_EN,C.T_TYPEID,C.T_FAULTID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID left join T_BASE_DISTRICT td on b.T_DISTRICTID=td.T_DISTRICTID left join T_INFO_PLANT tp on td.T_ORGID = tp.T_ORGID left join T_BASE_CITY CT on tp.T_CITYID= ct.T_CITYID where A.T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in({1}))) and A.ID_KEY not in (select ID_KEY from T_INFO_FAULTRECORD where T_ENDTIME <'{2}' or T_STARTTIME>'{3}') and C.T_TYPEID=(select T_TYPEID from T_BASE_FAULTTYPE where T_TYPEDESC='{4}') and A.ID_KEY not in(select top {5} ID_KEY from T_INFO_FAULTDETAIL order by ID_KEY desc)) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID where F.T_FAULTDESC_CN like '%{6}%' order by F.ID_KEY desc", count, stationId, sTime, eTime, faultType, count * (pageIndex - 1), title);
                        strSql = string.Format(@"select top {0} F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_PLANTDESC,F.T_CITYDESC,u.T_UNITDESC_CN,bt.T_TYPEDESC,F.T_FAULTID from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, tp.T_PLANTDESC,CT.T_CITYDESC, C.T_FAULTDESC_CN, C.T_FAULTDESC_EN,C.T_TYPEID,C.T_FAULTID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID left join T_BASE_DISTRICT td on b.T_DISTRICTID=td.T_DISTRICTID left join T_INFO_PLANT tp on td.T_ORGID = tp.T_ORGID left join T_BASE_CITY CT on tp.T_CITYID= ct.T_CITYID where A.T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in({1}))) and A.ID_KEY not in (select top {2} ID_KEY from T_INFO_FAULTDETAIL where T_ENDTIME is null or (T_STARTTIME between '{3}' and '{4}') order by ID_KEY desc)) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID where T_TYPEDESC='{5}' and F.T_FAULTDESC_CN like '%{6}% and (T_ENDTIME is null or (T_STARTTIME between '{7}' and '{8}')) order by F.ID_KEY desc", count, stationId, count * (pageIndex - 1), sTime, eTime, faultType, title, sTime, eTime);
                    else
                        strSql = string.Format(@"select top {0} F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_PLANTDESC,F.T_CITYDESC,u.T_UNITDESC_CN,bt.T_TYPEDESC,F.T_FAULTID from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, tp.T_PLANTDESC,CT.T_CITYDESC, C.T_FAULTDESC_CN, C.T_FAULTDESC_EN,C.T_TYPEID,C.T_FAULTID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID left join T_BASE_DISTRICT td on b.T_DISTRICTID=td.T_DISTRICTID left join T_INFO_PLANT tp on td.T_ORGID = tp.T_ORGID left join T_BASE_CITY CT on tp.T_CITYID= ct.T_CITYID where A.T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = '{1}')) and A.ID_KEY not in (select top {2} ID_KEY from T_INFO_FAULTDETAIL where T_ENDTIME is null or (T_STARTTIME between '{3}' and '{4}') order by ID_KEY desc)) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID where T_TYPEDESC='{5}' and F.T_FAULTDESC_CN like '%{6}% and (T_ENDTIME is null or (T_STARTTIME between '{7}' and '{8}')) order by F.ID_KEY desc", count, stationId, count * (pageIndex - 1), sTime, eTime, faultType, title, sTime, eTime);
                    //strSql = string.Format(@"select top {0} F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_PLANTDESC,F.T_CITYDESC,u.T_UNITDESC_CN,bt.T_TYPEDESC,F.T_FAULTID from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, tp.T_PLANTDESC,CT.T_CITYDESC,C.T_FAULTDESC_CN, C.T_FAULTDESC_EN,C.T_TYPEID,C.T_FAULTID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID left join T_BASE_DISTRICT td on b.T_DISTRICTID=td.T_DISTRICTID left join T_INFO_PLANT tp on td.T_ORGID = tp.T_ORGID    left join T_BASE_CITY CT on tp.T_CITYID= ct.T_CITYIDwhere A.T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = '{1}')) and A.ID_KEY not in (select ID_KEY from T_INFO_FAULTRECORD where T_ENDTIME <'{2}' or T_STARTTIME>'{3}') and C.T_TYPEID=(select T_TYPEID from T_BASE_FAULTTYPE where T_TYPEDESC='{4}') and A.ID_KEY not in(select top {5} ID_KEY from T_INFO_FAULTDETAIL order by ID_KEY desc)) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID where F.T_FAULTDESC_CN like '%{6}%' order by F.ID_KEY desc", count, stationId, sTime, eTime, faultType, count * (pageIndex - 1), title);
                }
                else
                {
                    if (stationId.IndexOf("'") > -1)
                        strSql = string.Format(@"select top {0} F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_PLANTDESC,F.T_CITYDESC,u.T_UNITDESC_CN,bt.T_TYPEDESC,F.T_FAULTID from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, tp.T_PLANTDESC,CT.T_CITYDESC, C.T_FAULTDESC_CN, C.T_FAULTDESC_EN,C.T_TYPEID,C.T_FAULTID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID left join T_BASE_DISTRICT td on b.T_DISTRICTID=td.T_DISTRICTID left join T_INFO_PLANT tp on td.T_ORGID = tp.T_ORGID left join T_BASE_CITY CT on tp.T_CITYID= ct.T_CITYID where A.T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in({1}))) and A.ID_KEY not in (select top {2} ID_KEY from T_INFO_FAULTDETAIL where T_ENDTIME is null or (T_STARTTIME between '{3}' and '{4}') order by ID_KEY desc)) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID where F.T_FAULTDESC_CN like '%{5}%' and (T_ENDTIME is null or (T_STARTTIME between '{6}' and '{7}')) order by F.ID_KEY desc", count, stationId, count * (pageIndex - 1), sTime, eTime, title, sTime, eTime);
                    //strSql = string.Format(@"select top {0} F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_PLANTDESC,F.T_CITYDESC,u.T_UNITDESC_CN,bt.T_TYPEDESC,F.T_FAULTID from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, tp.T_PLANTDESC,CT.T_CITYDESC,C.T_FAULTDESC_CN, C.T_FAULTDESC_EN,C.T_TYPEID,C.T_FAULTID From T_INFO_FAULTDETAIL A LEFT JOIN dbo.T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID left join T_BASE_DISTRICT td on b.T_DISTRICTID=td.T_DISTRICTID left join T_INFO_PLANT tp on td.T_ORGID = tp.T_ORGID left join T_BASE_CITY CT on tp.T_CITYID= ct.T_CITYID where A.T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in({1}))) and A.ID_KEY not in (select ID_KEY from T_INFO_FAULTRECORD where T_ENDTIME <'{2}' or T_STARTTIME>'{3}') and A.ID_KEY not in(select top {4} ID_KEY from T_INFO_FAULTDETAIL order by ID_KEY desc)) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID where F.T_FAULTDESC_CN like '%{5}%' order by F.ID_KEY desc", count, stationId, sTime, eTime, count * (pageIndex - 1), title);
                    else
                        strSql = string.Format(@"select top {0} F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_PLANTDESC,F.T_CITYDESC,u.T_UNITDESC_CN,bt.T_TYPEDESC,F.T_FAULTID from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, tp.T_PLANTDESC,CT.T_CITYDESC, C.T_FAULTDESC_CN, C.T_FAULTDESC_EN,C.T_TYPEID,C.T_FAULTID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID left join T_BASE_DISTRICT td on b.T_DISTRICTID=td.T_DISTRICTID left join T_INFO_PLANT tp on td.T_ORGID = tp.T_ORGID left join T_BASE_CITY CT on tp.T_CITYID= ct.T_CITYID where A.T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = '{1}')) and A.ID_KEY not in (select top {2} ID_KEY from T_INFO_FAULTDETAIL where T_ENDTIME is null or (T_STARTTIME between '{3}' and '{4}') order by ID_KEY desc)) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID where  F.T_FAULTDESC_CN like '%{5}%' and (T_ENDTIME is null or (T_STARTTIME between '{5}' and '{6}')) order by F.ID_KEY desc", count, stationId, count * (pageIndex - 1), sTime, eTime, title, sTime, eTime);
                    // strSql = string.Format(@"select top {0} F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_PLANTDESC,F.T_CITYDESC,u.T_UNITDESC_CN,bt.T_TYPEDESC,F.T_FAULTID from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, tp.T_PLANTDESC,CT.T_CITYDESC,C.T_FAULTDESC_CN, C.T_FAULTDESC_EN,C.T_TYPEID,C.T_FAULTID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID left join T_BASE_DISTRICT td on b.T_DISTRICTID=td.T_DISTRICTID left join T_INFO_PLANT tp on td.T_ORGID = tp.T_ORGID  left join T_BASE_CITY CT on tp.T_CITYID= ct.T_CITYID where A.T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = '{1}')) and A.ID_KEY not in (select ID_KEY from T_INFO_FAULTRECORD where T_ENDTIME <'{2}' or T_STARTTIME>'{3}') and A.ID_KEY not in(select top {4} ID_KEY from T_INFO_FAULTDETAIL order by ID_KEY desc)) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID where F.T_FAULTDESC_CN like '%{5}%' order by F.ID_KEY desc", count, stationId, sTime, eTime, count * (pageIndex - 1), title);
                }
            }
            else
            {
                if (faultType != "")
                {
                    if (stationId.IndexOf("'") > -1)
                        //strSql = string.Format(@"select top {0} F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_PLANTDESC,F.T_CITYDESC,u.T_UNITDESC_CN,bt.T_TYPEDESC,F.T_FAULTID from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, tp.T_PLANTDESC,CT.T_CITYDESC,C.T_FAULTDESC_CN, C.T_FAULTDESC_EN,C.T_TYPEID,C.T_FAULTID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID left join T_BASE_DISTRICT td on b.T_DISTRICTID=td.T_DISTRICTID left join T_INFO_PLANT tp on td.T_ORGID = tp.T_ORGID left join T_BASE_CITY CT on tp.T_CITYID= ct.T_CITYID where A.T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in({1}))) and A.ID_KEY not in (select ID_KEY from T_INFO_FAULTRECORD where T_ENDTIME <'{2}' or T_STARTTIME>'{3}')and C.T_TYPEID=(select T_TYPEID from T_BASE_FAULTTYPE where T_TYPEDESC='{4}')and A.ID_KEY not in(select top {5} ID_KEY from T_INFO_FAULTDETAIL order by ID_KEY desc)) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID order by F.ID_KEY desc", count, stationId, sTime, eTime, faultType, count * (pageIndex - 1));
                        strSql = string.Format(@"select top {0} F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_PLANTDESC,F.T_CITYDESC,u.T_UNITDESC_CN,bt.T_TYPEDESC,F.T_FAULTID from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, tp.T_PLANTDESC,CT.T_CITYDESC, C.T_FAULTDESC_CN, C.T_FAULTDESC_EN,C.T_TYPEID,C.T_FAULTID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID left join T_BASE_DISTRICT td on b.T_DISTRICTID=td.T_DISTRICTID left join T_INFO_PLANT tp on td.T_ORGID = tp.T_ORGID left join T_BASE_CITY CT on tp.T_CITYID= ct.T_CITYID where A.T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in({1}))) and A.ID_KEY not in (select top {2} ID_KEY from T_INFO_FAULTDETAIL where T_ENDTIME is null or (T_STARTTIME between '{3}' and '{4}') order by ID_KEY desc)) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID where T_TYPEDESC='{5}' and (T_ENDTIME is null or (T_STARTTIME between '{6}' and '{7}')) order by F.ID_KEY desc", count, stationId, count * (pageIndex - 1), sTime, eTime, faultType, sTime, eTime);
                    else
                        strSql = string.Format(@"select top {0} F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_PLANTDESC,F.T_CITYDESC,u.T_UNITDESC_CN,bt.T_TYPEDESC,F.T_FAULTID from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, tp.T_PLANTDESC,CT.T_CITYDESC, C.T_FAULTDESC_CN, C.T_FAULTDESC_EN,C.T_TYPEID,C.T_FAULTID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID left join T_BASE_DISTRICT td on b.T_DISTRICTID=td.T_DISTRICTID left join T_INFO_PLANT tp on td.T_ORGID = tp.T_ORGID left join T_BASE_CITY CT on tp.T_CITYID= ct.T_CITYID where A.T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = '{1}')) and A.ID_KEY not in (select top {2} ID_KEY from T_INFO_FAULTDETAIL where T_ENDTIME is null or (T_STARTTIME between '{3}' and '{4}') order by ID_KEY desc)) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID where  T_TYPEDESC='{5}' and (T_ENDTIME is null or (T_STARTTIME between '{6}' and '{7}')) order by F.ID_KEY desc", count, stationId, count * (pageIndex - 1), sTime, eTime, faultType, sTime, eTime);
                    //strSql = string.Format(@"select top {0} F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_PLANTDESC,F.T_CITYDESC,u.T_UNITDESC_CN,bt.T_TYPEDESC,F.T_FAULTID from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, tp.T_PLANTDESC,CT.T_CITYDESC,C.T_FAULTDESC_CN, C.T_FAULTDESC_EN,C.T_TYPEID,C.T_FAULTID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID left join T_BASE_DISTRICT td on b.T_DISTRICTID=td.T_DISTRICTID left join T_INFO_PLANT tp on td.T_ORGID = tp.T_ORGID left join T_BASE_CITY CT on tp.T_CITYID= ct.T_CITYID where A.T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = '{1}')) and A.ID_KEY not in (select ID_KEY from T_INFO_FAULTRECORD where T_ENDTIME <'{2}' or T_STARTTIME>'{3}') and C.T_TYPEID=(select T_TYPEID from T_BASE_FAULTTYPE where T_TYPEDESC='{4}') and A.ID_KEY not in(select top {5} ID_KEY from T_INFO_FAULTDETAIL order by ID_KEY desc)) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID order by F.ID_KEY desc", count, stationId, count * (pageIndex - 1), sTime, eTime, faultType, sTime, eTime);
                }
                else
                {
                    if (stationId.IndexOf("'") > -1)
                        //strSql = string.Format(@"select top {0} F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_PLANTDESC,F.T_CITYDESC,u.T_UNITDESC_CN,bt.T_TYPEDESC,F.T_FAULTID from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, tp.T_PLANTDESC,CT.T_CITYDESC, C.T_FAULTDESC_CN, C.T_FAULTDESC_EN,C.T_TYPEID,C.T_FAULTID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID left join T_BASE_DISTRICT td on b.T_DISTRICTID=td.T_DISTRICTID left join T_INFO_PLANT tp on td.T_ORGID = tp.T_ORGID left join T_BASE_CITY CT on tp.T_CITYID= ct.T_CITYID where A.T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in({1}))) and A.ID_KEY not in (select ID_KEY from T_INFO_FAULTRECORD where T_ENDTIME <'{2}' or T_STARTTIME>'{3}') and A.ID_KEY not in(select top {4} ID_KEY from T_INFO_FAULTDETAIL order by ID_KEY desc)) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID order by F.ID_KEY desc", count, stationId, sTime, eTime, count * (pageIndex - 1));
                        strSql = string.Format(@"select top {0} F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_PLANTDESC,F.T_CITYDESC,u.T_UNITDESC_CN,bt.T_TYPEDESC,F.T_FAULTID from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, tp.T_PLANTDESC,CT.T_CITYDESC, C.T_FAULTDESC_CN, C.T_FAULTDESC_EN,C.T_TYPEID,C.T_FAULTID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID left join T_BASE_DISTRICT td on b.T_DISTRICTID=td.T_DISTRICTID left join T_INFO_PLANT tp on td.T_ORGID = tp.T_ORGID left join T_BASE_CITY CT on tp.T_CITYID= ct.T_CITYID where A.T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in({1}))) and A.ID_KEY not in (select top {2} ID_KEY from T_INFO_FAULTDETAIL where T_ENDTIME is null or (T_STARTTIME between '{3}' and '{4}') order by ID_KEY desc)) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID where (T_ENDTIME is null or (T_STARTTIME between '{5}' and '{6}')) order by F.ID_KEY desc", count, stationId, count * (pageIndex - 1), sTime, eTime, sTime, eTime);
                    else
                        strSql = string.Format(@"select top {0} F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_PLANTDESC,F.T_CITYDESC,u.T_UNITDESC_CN,bt.T_TYPEDESC,F.T_FAULTID from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, tp.T_PLANTDESC,CT.T_CITYDESC, C.T_FAULTDESC_CN, C.T_FAULTDESC_EN,C.T_TYPEID,C.T_FAULTID From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID left join T_BASE_DISTRICT td on b.T_DISTRICTID=td.T_DISTRICTID left join T_INFO_PLANT tp on td.T_ORGID = tp.T_ORGID left join T_BASE_CITY CT on tp.T_CITYID= ct.T_CITYID where A.T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = '{1}')) and A.ID_KEY not in (select top {2} ID_KEY from T_INFO_FAULTDETAIL where T_ENDTIME is null or (T_STARTTIME between '{3}' and '{4}') order by ID_KEY desc)) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID where (T_ENDTIME is null or (T_STARTTIME between '{5}' and '{6}')) order by F.ID_KEY desc", count, stationId, count * (pageIndex - 1), sTime, eTime, sTime, eTime);
                    //strSql = string.Format(@"select top {0} F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,F.T_PLANTDESC,F.T_CITYDESC, u.T_UNITDESC_CN,bt.T_TYPEDESC,F.T_FAULTID from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID,tp.T_PLANTDESC, CT.T_CITYDESC,C.T_FAULTDESC_CN, C.T_FAULTDESC_EN,C.T_TYPEID,C.T_FAULTID From T_INFO_FAULTDETAIL A LEFT JOIN dbo.T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID left join T_BASE_DISTRICT td on b.T_DISTRICTID=td.T_DISTRICTID left join T_INFO_PLANT tp on td.T_ORGID = tp.T_ORGID left join T_BASE_CITY CT on tp.T_CITYID= ct.T_CITYID where A.T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = '{1}')) and A.ID_KEY not in (select ID_KEY from T_INFO_FAULTRECORD where T_ENDTIME <'{2}' or T_STARTTIME>'{3}') and A.ID_KEY not in(select top {4} ID_KEY from T_INFO_FAULTDETAIL order by ID_KEY desc)) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID left join T_BASE_FAULTTYPE bt on f.T_TYPEID = bt.T_TYPEID order by F.ID_KEY desc", count, stationId, sTime, eTime, count * (pageIndex - 1));
                }
            }
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }

        public int GetFaultDetailInfoCount(string stationId, DateTime sTime, DateTime eTime, string title, string faultType, int count, int pageIndex)
        {
            if (title != "")
            {
                if (faultType != "")
                {
                    if (stationId.IndexOf("'") > -1)
                        strSql = string.Format(@"select count(*) from (select F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,
                                    u.T_UNITDESC_CN from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, 
                                    C.T_FAULTDESC_CN, C.T_FAULTDESC_EN  From T_INFO_FAULTDETAIL A LEFT JOIN 
                                    dbo.T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=
                                    C.T_MACHINEID where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT 
                                    where T_ORGID in({0}))) and A.T_ENDTIME is null or (A.T_STARTTIME between '{1}' and '{2}') and C.T_TYPEID=(select T_TYPEID from T_BASE_FAULTTYPE where T_FAULTDESC_CN='{3}')) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID where F.T_FAULTDESC_CN like '%{4}%') a",
                                                 stationId, sTime, eTime, faultType, title);
                    else
                        strSql = string.Format(@"select count(*) from (select F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,
                                    u.T_UNITDESC_CN from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, 
                                    C.T_FAULTDESC_CN, C.T_FAULTDESC_EN  From T_INFO_FAULTDETAIL A LEFT JOIN 
                                    dbo.T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=
                                    C.T_MACHINEID where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT 
                                    where T_ORGID = '{0}')) and A.T_ENDTIME is null or (A.T_STARTTIME between '{3}' and '{4}') and C.T_TYPEID=(select T_TYPEID from T_BASE_FAULTTYPE where T_FAULTDESC_CN='{3}')) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID where F.T_FAULTDESC_CN like '%{4}%') a", stationId, sTime, eTime, faultType, title);
                }
                else
                {
                    if (stationId.IndexOf("'") > -1)
                        strSql = string.Format(@"select count(*) from (select F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,
                                    u.T_UNITDESC_CN from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, 
                                    C.T_FAULTDESC_CN, C.T_FAULTDESC_EN  From T_INFO_FAULTDETAIL A LEFT JOIN 
                                    dbo.T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=
                                    C.T_MACHINEID where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT 
                                    where T_ORGID in({0})))  and A.T_ENDTIME is null or (A.T_STARTTIME between '{1}' and '{2}')) 
                F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID where F.T_FAULTDESC_CN like '%{3}%') a", stationId, sTime, eTime, sTime, eTime, title);

                    else
                        strSql = string.Format(@"select count(*) from (select F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,
                                    u.T_UNITDESC_CN from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, 
                                    C.T_FAULTDESC_CN, C.T_FAULTDESC_EN  From T_INFO_FAULTDETAIL A LEFT JOIN 
                                    dbo.T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=
                                    C.T_MACHINEID where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT 
                                    where T_ORGID = '{0}')) and A.T_ENDTIME is null or (A.T_STARTTIME between '{1}' and '{2}')) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID where F.T_FAULTDESC_CN like '%{3}%') a",
                                                stationId, sTime, eTime, title);
                }
            }
            else
            {
                if (faultType != "")
                {
                    if (stationId.IndexOf("'") > -1)
                        strSql = string.Format(@"select count(*) from (select F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,u.T_UNITDESC_CN from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, C.T_FAULTDESC_CN, C.T_FAULTDESC_EN From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in({0})))  and A.T_ENDTIME is null or (A.T_STARTTIME between '{1}' and '{2}') and C.T_TYPEID=(select T_TYPEID from T_BASE_FAULTTYPE  where T_FAULTDESC_CN='{3}')) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID) a", stationId, sTime, eTime, faultType);
                    else
                        strSql = string.Format(@"select count(*) from (select F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,u.T_UNITDESC_CN from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, C.T_FAULTDESC_CN, C.T_FAULTDESC_EN From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = '{0}'))  and A.T_ENDTIME is null or (A.T_STARTTIME between '{1}' and '{2}') and C.T_TYPEID=(select T_TYPEID from T_BASE_FAULTTYPE  where T_FAULTDESC_CN='{3}')) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID) a", stationId, sTime, eTime, faultType);
                }
                else
                {
                    if (stationId.IndexOf("'") > -1)
                        strSql = string.Format(@"select count(*) from (select F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,u.T_UNITDESC_CN from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, C.T_FAULTDESC_CN, C.T_FAULTDESC_EN From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID=C.T_MACHINEID where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in({0})))  and A.T_ENDTIME is null or (A.T_STARTTIME between '{1}' and '{2}')) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID) a", stationId, sTime, eTime);
                    else
                        strSql = string.Format(@"select count(*) from (select F.ID_KEY, F.T_STARTTIME,F.T_ENDTIME,F.T_ORGID,F.T_FAULTDESC_CN,F.T_FAULTDESC_EN,u.T_UNITDESC_CN from(select T_WATCHID, T_STARTTIME, T_ENDTIME, A.ID_KEY, A.T_ORGID, C.T_FAULTDESC_CN, C.T_FAULTDESC_EN From T_INFO_FAULTDETAIL A LEFT JOIN T_BASE_UNIT B ON A.T_ORGID= B.T_UNITID LEFT JOIN dbo.T_BASE_FAULTPARA C ON A.T_WATCHID= C.T_FAULTID AND B.T_MACHINEID= C.T_MACHINEID where T_ORGID in(select T_UNITID from T_BASE_UNIT where T_DISTRICTID in(select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID = '{0}'))  and A.T_ENDTIME is null or (A.T_STARTTIME between '{1}' and '{2}')) F inner join T_BASE_UNIT u on F.T_ORGID = u.T_UNITID) a", stationId, sTime, eTime);
                }
            }
            return (int)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunRowCount", new object[] { strSql, strErrMsg });
        }




        #endregion

        #region 获取电站与组织机构关系 电站上一级组织机构  市
        public DataTable GetCityID(string stationID)
        {
            if (stationID == "")
                strSql = "select * from T_BASE_CITY";
            else if (stationID.IndexOf("'") > -1)
                strSql = "select T_CITYID from T_INFO_PLANT where T_ORGID in(" + stationID + ")";
            else
                strSql = "select T_CITYID from T_INFO_PLANT where T_ORGID ='" + stationID + "'";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            return dt;
        }
        #endregion

        #region 同工况下时间范围内发电量
        public double GetCconditionDL(string id, DateTime st, DateTime et)
        {
            double dVal = 0;
            double dDL = GetStationDL(id, st.ToString(), et.ToString());
            strSql = string.Format(@"select isnull(avg(D_VALUE),0) value from T_INFO_STATISTICS where T_TJID ='PJFZD' and T_TIME between '{0}' and '{1}' and T_ORGID='{2}'", st, et, id);
            double avgFZD = Convert.ToDouble(iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg }).ToString());

            strSql = string.Format(@"select isnull(avg(D_VALUE),0) value from T_INFO_STATISTICS where T_TJID ='PJWD' and T_TIME between '{0}' and '{1}' and T_ORGID='{2}'", st, et, id);
            double avgTemperature = Convert.ToDouble(iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg }).ToString());
            if (avgTemperature - 25 < 0)
                avgTemperature = -(avgTemperature - 25);
            double rl = GetStationRL(id);
            double zjjd = 0;
            strSql = string.Format(@"select T_PANELCOE from T_INFO_PLANT where T_ORGID='{0}'", id);
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            if (obj != null || obj == "") zjjd = Convert.ToDouble(obj.ToString());

            if (rl == 0 || avgTemperature == 0 || avgFZD == 0 || zjjd == 0)
                return dVal;
            else
            {
                if (0.0035 * avgTemperature != 1)
                    return dDL / rl * 1000 / zjjd / (1 - 0.0035 * avgTemperature) / zjjd;
                else
                    return dVal;
            }
        }
        #endregion

        #region 电站电量统计
        public DataTable GetStationYearTotalDLByTime(string id, DateTime st, DateTime et)
        {
            if (id != "")
                strSql = string.Format(@"select datepart(YEAR, T_TIME) as 年,sum(D_VALUE) as 合计 from T_INFO_STATISTICS where T_ORGID in(select T_DISTRICTID From T_BASE_DISTRICT where T_ORGID= '{0}') and T_TJID='DL' and T_TIME between '{1}' and '{2}' group by datepart(YEAR, T_TIME) order by 年 asc", id, st, et);
            else
                strSql = string.Format(@"select datepart(YEAR, T_TIME) as 年,sum(D_VALUE) as 合计 from T_INFO_STATISTICS where T_ORGID in(select T_DISTRICTID From T_BASE_DISTRICT) and T_TJID='DL' and T_TIME between '{1}' and '{2}' group by datepart(YEAR, T_TIME) order by 年 asc", id, st, et);
            return (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
        }

        public DataTable GetStationMonthTotalDLByTime(string id, DateTime st, DateTime et)
        {
            if (id != "")
                strSql = string.Format(@"select datepart(YEAR, T_TIME) as 年,datepart(month, T_TIME) as 月,sum(D_VALUE) as 合计 from T_INFO_STATISTICS where T_ORGID in(select T_DISTRICTID From T_BASE_DISTRICT where T_ORGID= '{0}') and T_TJID='DL' and T_TIME between '{1}' and '{2}' group by datepart(YEAR, T_TIME), datepart(month, T_TIME) order by 年,月 asc", id, st, et);
            else
                strSql = string.Format(@"select datepart(YEAR, T_TIME) as 年,datepart(month, T_TIME) as 月,sum(D_VALUE) as 合计 from T_INFO_STATISTICS where T_ORGID in(select T_DISTRICTID From T_BASE_DISTRICT) and T_TJID='DL' and T_TIME between '{1}' and '{2}' group by datepart(YEAR, T_TIME), datepart(month, T_TIME) order by 年,月 asc", id, st, et);
            return (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
        }

        public DataTable GetStationDayTotalDLByTime(string id, DateTime st, DateTime et)
        {
            if (id != "")
                strSql = string.Format(@"select datepart(YEAR, T_TIME) as 年,datepart(month, T_TIME) as 月,datepart(day, T_TIME) as 日,sum(D_VALUE) as 合计 from T_INFO_STATISTICS where T_ORGID in(select T_DISTRICTID From T_BASE_DISTRICT where T_ORGID= '{0}') and T_TJID='DL' and T_TIME between '{1}' and '{2}' group by datepart(YEAR, T_TIME),datepart(month, T_TIME),datepart(day, T_TIME) order by 年,月,日 asc", id, st, et);
            else
                strSql = string.Format(@"select datepart(YEAR, T_TIME) as 年,datepart(month, T_TIME) as 月,datepart(day, T_TIME) as 日,sum(D_VALUE) as 合计 from T_INFO_STATISTICS where T_ORGID in(select T_DISTRICTID From T_BASE_DISTRICT) and T_TJID='DL' and T_TIME between '{1}' and '{2}' group by datepart(YEAR, T_TIME),datepart(month, T_TIME),datepart(day, T_TIME) order by 年,月,日 asc", id, st, et);
            return (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
        }

        public DataTable GetStationHourTotalDLByTime(string id, DateTime st, DateTime et)
        {
            if (id != "")
                strSql = string.Format(@"select datepart(YEAR, T_TIME) as 年,datepart(month, T_TIME) as 月,datepart(day, T_TIME) as 日,datepart(hour, T_TIME) as 小时,sum(D_VALUE) as 合计 from T_INFO_STATISTICS where T_ORGID in(select T_DISTRICTID From T_BASE_DISTRICT where T_ORGID= '{0}') and T_TJID='DL' and T_TIME between '{1}' and '{2}' group by datepart(YEAR, T_TIME),datepart(month, T_TIME),datepart(day, T_TIME),datepart(hour, T_TIME) order by 年,月,日,小时 asc", id, st, et);
            else
                strSql = string.Format(@"select datepart(YEAR, T_TIME) as 年,datepart(month, T_TIME) as 月,datepart(day, T_TIME) as 日,datepart(hour, T_TIME) as 小时,sum(D_VALUE) as 合计 from T_INFO_STATISTICS where T_ORGID in(select T_DISTRICTID From T_BASE_DISTRICT) and T_TJID='DL' and T_TIME between '{1}' and '{2}' group by datepart(YEAR, T_TIME),datepart(month, T_TIME),datepart(day, T_TIME),datepart(hour, T_TIME) order by 年,月,日,小时 asc", id, st, et);
            return (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
        }
        #endregion

        #region 累计时间范围内平均功率
        public DataTable GetStationYearAvgPowerByTime(string id, DateTime st, DateTime et)
        {
            strSql = string.Format(@"select datepart(YEAR, T_TIME) as 年,avg(D_VALUE) as 合计 from T_INFO_STATISTICS where T_ORGID in(select T_DISTRICTID From T_BASE_DISTRICT where T_ORGID= '{0}') and T_TJID='PJGL' and T_TIME between '{1}' and '{2}' group by datepart(YEAR, T_TIME) order by 年 asc", id, st, et);
            return (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
        }
        public DataTable GetStationMonthAvgPowerByTime(string id, DateTime st, DateTime et)
        {
            strSql = string.Format(@"select datepart(YEAR, T_TIME) as 年,datepart(month, T_TIME) as 月,avg(D_VALUE) as 合计 from T_INFO_STATISTICS where T_ORGID in(select T_DISTRICTID From T_BASE_DISTRICT where T_ORGID= '{0}') and T_TJID='PJGL' and T_TIME between '{1}' and '{2}' group by datepart(YEAR, T_TIME), datepart(month, T_TIME) order by 年,月 asc", id, st, et);
            return (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
        }
        public DataTable GetStationDayAvgPowerByTime(string id, DateTime st, DateTime et)
        {
            strSql = string.Format(@"select datepart(YEAR, T_TIME) as 年,datepart(month, T_TIME) as 月,datepart(day, T_TIME) as 日,avg(D_VALUE) as 合计 from T_INFO_STATISTICS where T_ORGID in(select T_DISTRICTID From T_BASE_DISTRICT where T_ORGID= '{0}') and T_TJID='PJGL' and T_TIME between '{1}' and '{2}' group by datepart(YEAR, T_TIME),datepart(month, T_TIME),datepart(day, T_TIME) order by 年,月,日 asc", id, st, et);
            return (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
        }
        public DataTable GetStationHourAvgPowerByTime(string id, DateTime st, DateTime et)
        {
            strSql = string.Format(@"select datepart(YEAR, T_TIME) as 年,datepart(month, T_TIME) as 月,datepart(day, T_TIME) as 日,datepart(hour, T_TIME) as 小时,avg(D_VALUE) as 合计 from T_INFO_STATISTICS where T_ORGID in(select T_DISTRICTID From T_BASE_DISTRICT where T_ORGID= '{0}') and T_TJID='PJGL' and T_TIME between '{1}' and '{2}' group by datepart(YEAR, T_TIME),datepart(month, T_TIME),datepart(day, T_TIME),datepart(hour, T_TIME) order by 年,月,日,小时 asc", id, st, et);
            return (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
        }
        #endregion

        #region 累计发电量
        public double GetSumDL(string id)
        {
            strSql = "select T_TOTALELECTAG from T_BASE_POINTS_ORG where T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            if (obj != null && obj != DBNull.Value)
                if (rtdbJudge == "1")
                    return GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] { obj.ToString() })), 2);
                else
                    return GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValueNoJudge", new object[] { obj.ToString() })), 2);
            else
                return Double.NaN;
        }
        #endregion

        #region 获取

        #endregion

        //#region  获取区域电量信息 -单云龙
        //public DataTable GetDLByPid(string sid, string pid, string dt, string type)
        //{
        //    string errMsg, sql, qsrq, jsrq;

        //    DataTable dtDL = new DataTable();

        //    DataColumn dc1 = new DataColumn("sBM", typeof(int));
        //    DataColumn dc2 = new DataColumn("sID", typeof(string));
        //    DataColumn dc3 = new DataColumn("sName", typeof(string));
        //    DataColumn dc4 = new DataColumn("pid", typeof(string));
        //    DataColumn dc5 = new DataColumn("pName", typeof(string));
        //    DataColumn dc6 = new DataColumn("dt", typeof(string));
        //    DataColumn dc7 = new DataColumn("value", typeof(string));

        //    dtDL.Columns.Add(dc1);
        //    dtDL.Columns.Add(dc2);
        //    dtDL.Columns.Add(dc3);
        //    dtDL.Columns.Add(dc4);
        //    dtDL.Columns.Add(dc5);
        //    dtDL.Columns.Add(dc6);
        //    dtDL.Columns.Add(dc7);

        //    qsrq = DateTime.Parse(dt).ToString("yyyy-MM-dd 0:00:00");
        //    jsrq = DateTime.Parse(dt).ToString("yyyy-MM-dd 23:59:59");


        //    if (pid != "")
        //    {
        //        sql = string.Format("select p.t_orgid,p.t_plantdesc,d.t_districtid,d.t_districtdesc_cn from T_INFO_PLANT as p INNER JOIN T_BASE_DISTRICT as d on p.t_orgid = d.t_orgid where d.T_DISTRICTID = '{0}'", pid);



        //        //   DataRow drIDs = db.RunDataRow(sql, out errMsg);

        //        //由于WebService 不能序列化参数，所以进行修改

        //        DataRow drIDs = ((DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { sql, strErrMsg })).Rows[0];

        //        if (type == "日")
        //        {
        //            sql = string.Format("select sum(d_value) as value from T_INFO_STATISTICS where T_TJID='DL' and  T_ORGID='{0}' and t_time>='{1}' and t_time<'{2}'", drIDs["t_districtid"].ToString(), qsrq, jsrq);

        //            object obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { sql, strErrMsg });

        //            DataRow dr = dtDL.NewRow();

        //            dr[0] = 1;
        //            dr[1] = drIDs["t_orgid"];
        //            dr[2] = drIDs["t_plantdesc"];
        //            dr[3] = drIDs["t_districtid"];
        //            dr[4] = drIDs["t_districtdesc_cn"];
        //            dr[5] = DateTime.Parse(dt).ToString("yyyy-MM-dd");

        //            if (obj != null && obj.ToString() != "")
        //                dr[6] = obj.ToString();
        //            else
        //                dr[6] = "";
        //            dtDL.Rows.Add(dr);
        //        }
        //        else if (type == "小时")
        //        {
        //            for (int i = 0; i < 24; i++)
        //            {
        //                qsrq = DateTime.Parse(dt).AddHours(+i).ToString("yyyy-MM-dd H:mm:ss");

        //                sql = string.Format("select sum(d_value) as value from T_INFO_STATISTICS where T_TJID='DL' and  T_ORGID='{0}' and t_time='{1}'", drIDs["t_districtid"].ToString(), qsrq);

        //                object obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { sql, strErrMsg });

        //                DataRow dr = dtDL.NewRow();

        //                dr[0] = 1;
        //                dr[1] = drIDs["t_orgid"];
        //                dr[2] = drIDs["t_plantdesc"];
        //                dr[3] = drIDs["t_districtid"];
        //                dr[4] = drIDs["t_districtdesc_cn"];
        //                dr[5] = DateTime.Parse(dt).AddHours(+i).ToString("yyyy-MM-dd H:00:00");

        //                if (obj != null && obj.ToString() != "")
        //                    dr[6] = obj.ToString();
        //                else
        //                    dr[6] = "";

        //                dtDL.Rows.Add(dr);
        //            }
        //        }
        //    }
        //    return dtDL;
        //}

        //#endregion

        //#region  更新区域电量信息 -单云龙
        //public bool SetListStatics(string sid, string pid, string dt, string type, string value)
        //{
        //    //sid = Request["sid"];
        //    //pid = Request["pid"];
        //    //type = Request["type"];
        //    //time = Request["time"];
        //    //value = Request["value"];

        //    bool flag = false;
        //    string errMsg, sql, qsrq, jsrq;

        //    if (value != "")
        //    {
        //        //val0 - 22.0994873049785;
        //        double d_value = 0;
        //        string[] arrValue;

        //        //查出期下边的逆变器
        //        sql = string.Format("select * from t_base_unit where t_districtid='{0}' and t_machineid in (select t_machineid from T_BASE_MACHINE where T_TYPEID='nbq')", pid);

        //        DataTable dtUnit = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { sql, strErrMsg });
        //        sql = "";
        //        if (type == "日")
        //        {
        //            arrValue = value.TrimEnd(';').Split(';');

        //            if (arrValue[0].Split('-')[1] != null && arrValue[0].Split('-')[1].ToString() != "")
        //            {
        //                d_value = double.Parse(arrValue[0].Split('-')[1]);

        //                //更新工期数据
        //                double p_value = 0;
        //                double y_value = 0;
        //                double u_value = 0;

        //                double p_fact = 1;

        //                if (d_value < 24)
        //                    p_fact = Math.Ceiling(24 / d_value);

        //                if (p_fact != 0)
        //                    u_value = d_value * p_fact / 24;
        //                else
        //                    u_value = d_value / 24;

        //                for (int i = 0; i < 24; i++)
        //                {
        //                    y_value = u_value * 23;

        //                    string time = DateTime.Parse(dt).AddHours(+i).ToString("yyyy-MM-dd H:00:00");

        //                    if (i == 23)
        //                    { p_value = d_value - y_value / p_fact; }
        //                    else
        //                    { p_value = u_value / p_fact; }

        //                    sql += string.Format("update T_INFO_STATISTICS set d_value={0} where t_tjid='DL' and t_orgid='{1}' and t_time ='{2}'", p_value, pid, time);

        //                    if (dtUnit != null && dtUnit.Rows.Count > 0)
        //                    {
        //                        double uu_value = 0;
        //                        double p_y_value = 0;
        //                        double u_p_fact = 1;
        //                        double p_u_value = 0;

        //                        if (p_value < dtUnit.Rows.Count)
        //                            u_p_fact = Math.Ceiling(dtUnit.Rows.Count / p_value);

        //                        if (p_fact != 0)
        //                            p_u_value = p_value * u_p_fact / dtUnit.Rows.Count;
        //                        else
        //                            p_u_value = p_value / dtUnit.Rows.Count;

        //                        for (int j = 0; j < dtUnit.Rows.Count; j++)
        //                        {
        //                            p_y_value = p_u_value * (dtUnit.Rows.Count - 1);

        //                            if (j == dtUnit.Rows.Count - 1)
        //                                uu_value = p_value - p_y_value / u_p_fact;
        //                            else
        //                                uu_value = p_u_value / u_p_fact;

        //                            sql += string.Format("update T_INFO_STATISTICS set d_value={0} where t_tjid='DL' and t_orgid='{1}' and t_time ='{2}'", uu_value, dtUnit.Rows[j]["t_unitid"].ToString(), time);
        //                        }
        //                    }
        //                }
        //                flag = (bool)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunNonQuery", new object[] { sql, strErrMsg });
        //            }
        //        }
        //        else if (type == "小时")
        //        {
        //            sql = "";
        //            arrValue = value.TrimEnd(';').Split(';');

        //            for (int k = 0; k < arrValue.Length; k++)
        //            {
        //                string time = DateTime.Parse(dt).AddHours(+int.Parse(arrValue[k].Split('-')[0].Replace("val", ""))).ToString("yyyy-MM-dd H:00:00");
        //                //对每个小时数据赋值
        //                if (arrValue[k].Split('-')[1] != null && arrValue[k].Split('-')[1].ToString() != "")
        //                {
        //                    d_value = double.Parse(arrValue[k].Split('-')[1]);
        //                    //更新期数小时数据
        //                    sql += string.Format("update T_INFO_STATISTICS set d_value={0} where t_tjid='DL' and t_orgid='{1}' and t_time ='{2}'", d_value, pid, time);

        //                    //flag = db.RunNonQuery(sql, out errMsg);

        //                    //更新逆变器电量
        //                    if (dtUnit != null && dtUnit.Rows.Count > 0)
        //                    {
        //                        double p_fact = 1;
        //                        double u_value = 0;

        //                        if (d_value < dtUnit.Rows.Count)
        //                            p_fact = Math.Ceiling(dtUnit.Rows.Count / d_value);

        //                        if (p_fact != 0)
        //                            u_value = d_value * p_fact / dtUnit.Rows.Count;
        //                        else
        //                            u_value = d_value / dtUnit.Rows.Count;

        //                        for (int i = 0; i < dtUnit.Rows.Count; i++)
        //                        {
        //                            sql += string.Format("update T_INFO_STATISTICS set d_value={0} where t_tjid='DL' and t_orgid='{1}' and t_time ='{2}'", u_value / p_fact, dtUnit.Rows[i]["t_unitid"].ToString(), time);
        //                        }
        //                    }
        //                }
        //            }
        //            flag = (bool)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunNonQuery", new object[] { sql, strErrMsg });
        //        }
        //    }
        //    return flag;
        //}

        //#endregion


        #region  获取区域电量信息 -单云龙
        public DataTable GetDLByPid(string sid, string pid, string dt, string type)
        {
            string errMsg, sql, qsrq, jsrq;

            DataTable dtDL = new DataTable();

            DataColumn dc1 = new DataColumn("sBM", typeof(int));
            DataColumn dc2 = new DataColumn("sID", typeof(string));
            DataColumn dc3 = new DataColumn("sName", typeof(string));
            DataColumn dc4 = new DataColumn("pid", typeof(string));
            DataColumn dc5 = new DataColumn("pName", typeof(string));
            DataColumn dc6 = new DataColumn("dt", typeof(string));
            DataColumn dc7 = new DataColumn("value", typeof(string));

            dtDL.Columns.Add(dc1);
            dtDL.Columns.Add(dc2);
            dtDL.Columns.Add(dc3);
            dtDL.Columns.Add(dc4);
            dtDL.Columns.Add(dc5);
            dtDL.Columns.Add(dc6);
            dtDL.Columns.Add(dc7);

            qsrq = DateTime.Parse(dt).ToString("yyyy-MM-dd 0:00:00");
            jsrq = DateTime.Parse(dt).ToString("yyyy-MM-dd 23:59:59");


            if (pid != "")
            {
                sql = string.Format("select p.t_orgid,p.t_plantdesc,d.t_districtid,d.t_districtdesc_cn from T_INFO_PLANT as p INNER JOIN T_BASE_DISTRICT as d on p.t_orgid = d.t_orgid where d.T_DISTRICTID = '{0}'", pid);



                //   DataRow drIDs = db.RunDataRow(sql, out errMsg);

                //由于WebService 不能序列化参数，所以进行修改

                DataRow drIDs = ((DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { sql, strErrMsg })).Rows[0];

                if (type == "日")
                {
                    sql = string.Format("select sum(d_value) as value from T_INFO_STATISTICS where T_TJID='DL' and  T_ORGID='{0}' and t_time>='{1}' and t_time<'{2}'", drIDs["t_districtid"].ToString(), qsrq, jsrq);

                    object obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { sql, strErrMsg });

                    DataRow dr = dtDL.NewRow();

                    dr[0] = 1;
                    dr[1] = drIDs["t_orgid"];
                    dr[2] = drIDs["t_plantdesc"];
                    dr[3] = drIDs["t_districtid"];
                    dr[4] = drIDs["t_districtdesc_cn"];
                    dr[5] = DateTime.Parse(dt).ToString("yyyy-MM-dd");

                    if (obj != null && obj.ToString() != "")
                        dr[6] = obj.ToString();
                    else
                        dr[6] = "";
                    dtDL.Rows.Add(dr);
                }
                else if (type == "小时")
                {
                    for (int i = 0; i < 24; i++)
                    {
                        qsrq = DateTime.Parse(dt).AddHours(+i).ToString("yyyy-MM-dd H:mm:ss");

                        sql = string.Format("select sum(d_value) as value from T_INFO_STATISTICS where T_TJID='DL' and  T_ORGID='{0}' and t_time='{1}'", drIDs["t_districtid"].ToString(), qsrq);

                        object obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { sql, strErrMsg });

                        DataRow dr = dtDL.NewRow();

                        dr[0] = 1;
                        dr[1] = drIDs["t_orgid"];
                        dr[2] = drIDs["t_plantdesc"];
                        dr[3] = drIDs["t_districtid"];
                        dr[4] = drIDs["t_districtdesc_cn"];
                        dr[5] = DateTime.Parse(dt).AddHours(+i).ToString("yyyy-MM-dd H:00:00");

                        if (obj != null && obj.ToString() != "")
                            dr[6] = obj.ToString();
                        else
                            dr[6] = "";

                        dtDL.Rows.Add(dr);
                    }
                }
            }
            return dtDL;
        }

        #endregion

        #region  更新区域电量信息 -单云龙
        public bool SetListStatics(string sid, string pid, string dt, string type, string value)
        {
            //sid = Request["sid"];
            //pid = Request["pid"];
            //type = Request["type"];
            //time = Request["time"];
            //value = Request["value"];

            bool flag = false;
            string errMsg, sql, qsrq, jsrq;

            if (value != "")
            {
                //val0 - 22.0994873049785;
                double d_value = 0;
                string[] arrValue;

                //查出期下边的逆变器
                sql = string.Format("select * from t_base_unit where t_districtid='{0}' and t_machineid in (select t_machineid from T_BASE_MACHINE where T_TYPEID='nbq')", pid);

                DataTable dtUnit = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { sql, strErrMsg });
                sql = "";
                if (type == "日")
                {
                    arrValue = value.TrimEnd(';').Split(';');

                    if (arrValue[0].Split('-')[1] != null && arrValue[0].Split('-')[1].ToString() != "")
                    {
                        d_value = double.Parse(arrValue[0].Split('-')[1]);

                        //更新工期数据
                        double p_value = 0;
                        double y_value = 0;
                        double u_value = 0;

                        double p_fact = 1;

                        if (d_value != 0 && d_value < 24)
                            p_fact = Math.Ceiling(24 / d_value);

                        if (p_fact != 0)
                            u_value = d_value * p_fact / 24;
                        else
                            u_value = d_value / 24;

                        for (int i = 0; i < 24; i++)
                        {
                            y_value = u_value * 23;

                            string time = DateTime.Parse(dt).AddHours(+i).ToString("yyyy-MM-dd H:00:00");

                            if (i == 23)
                            { p_value = d_value - y_value / p_fact; }
                            else
                            { p_value = u_value / p_fact; }

                            //sql += string.Format("update T_INFO_STATISTICS set d_value={0} where t_tjid='DL' and t_orgid='{1}' and t_time ='{2}' ", p_value, pid, time);
                            sql += string.Format("delete T_INFO_STATISTICS where t_tjid='DL' and t_orgid='{1}' and t_time ='{2}';insert into T_INFO_STATISTICS values('DL','{1}','{2}',{0}); ", p_value, pid, time);
                            //
                            if (dtUnit != null && dtUnit.Rows.Count > 0)
                            {
                                double uu_value = 0;
                                double p_y_value = 0;
                                double u_p_fact = 1;
                                double p_u_value = 0;

                                if (p_value != 0 && p_value < dtUnit.Rows.Count)
                                    u_p_fact = Math.Ceiling(dtUnit.Rows.Count / p_value);

                                if (p_fact != 0)
                                    p_u_value = p_value * u_p_fact / dtUnit.Rows.Count;
                                else
                                    p_u_value = p_value / dtUnit.Rows.Count;

                                for (int j = 0; j < dtUnit.Rows.Count; j++)
                                {
                                    p_y_value = p_u_value * (dtUnit.Rows.Count - 1);

                                    if (j == dtUnit.Rows.Count - 1)
                                        uu_value = p_value - p_y_value / u_p_fact;
                                    else
                                        uu_value = p_u_value / u_p_fact;

                                    //sql += string.Format("update T_INFO_STATISTICS set d_value={0} where t_tjid='DL' and t_orgid='{1}' and t_time ='{2}' ", uu_value, dtUnit.Rows[j]["t_unitid"].ToString(), time);
                                    sql += string.Format("delete T_INFO_STATISTICS where t_tjid='DL' and t_orgid='{1}' and t_time ='{2}';insert into T_INFO_STATISTICS values('DL','{1}','{2}',{0}); ", uu_value, dtUnit.Rows[j]["t_unitid"].ToString(), time);
                                }
                            }
                        }
                        flag = (bool)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunNonQuery", new object[] { sql, strErrMsg });
                    }
                }
                else if (type == "小时")
                {
                    arrValue = value.TrimEnd(';').Split(';');

                    for (int k = 0; k < arrValue.Length; k++)
                    {
                        string time = DateTime.Parse(dt).AddHours(+int.Parse(arrValue[k].Split('-')[0].Replace("val", ""))).ToString("yyyy-MM-dd H:00:00");
                        //对每个小时数据赋值
                        if (arrValue[k].Split('-')[1] != null && arrValue[k].Split('-')[1].ToString() != "")
                        {
                            d_value = double.Parse(arrValue[k].Split('-')[1]);
                            //更新期数小时数据
                            sql += string.Format("delete T_INFO_STATISTICS where t_tjid='DL' and t_orgid='{1}' and t_time ='{2}';insert into T_INFO_STATISTICS values('DL','{1}','{2}',{0});", d_value, pid, time);

                            //flag = db.RunNonQuery(sql, out errMsg);

                            //更新逆变器电量
                            if (dtUnit != null && dtUnit.Rows.Count > 0)
                            {
                                double p_fact = 1;
                                double u_value = 0;

                                if (d_value != 0 && d_value < dtUnit.Rows.Count)
                                    p_fact = Math.Ceiling(dtUnit.Rows.Count / d_value);

                                if (p_fact != 0)
                                    u_value = d_value * p_fact / dtUnit.Rows.Count;
                                else
                                    u_value = d_value / dtUnit.Rows.Count;

                                for (int i = 0; i < dtUnit.Rows.Count; i++)
                                {
                                    sql += string.Format("delete T_INFO_STATISTICS where t_tjid='DL' and t_orgid='{1}' and t_time ='{2}';insert into T_INFO_STATISTICS values('DL','{1}','{2}',{0});", u_value / p_fact, dtUnit.Rows[i]["t_unitid"].ToString(), time);
                                }
                            }
                        }
                    }
                    flag = (bool)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunNonQuery", new object[] { sql, strErrMsg });
                }
            }
            return flag;
        }

        #endregion

        #region 获取电站图片
        public object GetStationImg(string id)
        {
            strSql = "select T_IMGURL from T_INFO_PLANT where T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            return obj;
        }
        #endregion

        #region 获取电站名称
        public DataTable GetStationNameByName(string user, string name)
        {
            strSql = "select T_ORGID,T_PLANTDESC from T_INFO_PLANT where T_ORGID in(select T_ORGID From T_SYS_MEMBERORG where T_USERID='" + user + "') and T_PLANTDESC like '%" + name + "%'";
            //获取工期编码
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });

            return dt;
        }
        #endregion      

        #region  获取计算测点合中文描述集合 -单云龙
        public DataTable GetListForTPTInfo()
        {
            string sql = string.Format("select * from t_base_calcpara");

            DataTable dtInfo = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { sql, strErrMsg });

            return dtInfo;
        }

        #endregion

        #region  根据TAGID获取底层数据 -单云龙

        string strTag = "";
        DataTable ResultData = null;

        public DataTable GetTableForTPTree(string strInput)
        {
            ResultData = new DataTable();
            ResultData.Columns.Add("ID");
            ResultData.Columns.Add("PID");
            ResultData.Columns.Add("DESC");//=ID,desc
            ResultData.Columns.Add("VALUE");//=ID,desc
            ResultData.Columns.Add("FORMULA");//=ID,desc
            ResultData.Columns.Add("Level");//=ID,desc

            string sql = string.Format("select * from t_base_calcpara");

            DataTable dtInfo = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { sql, strErrMsg });

            if (dtInfo != null && dtInfo.Rows.Count > 0)
            {
                string[] tagsName = null;
                string[] strArr = strInput.Split(',');

                //添加父节点
                DataRow dr = ResultData.NewRow();

                dr["ID"] = strArr[0];
                dr["PID"] = -1;
                dr["DESC"] = strArr[1];
                dr["VALUE"] = "";
                dr["FORMULA"] = "";//=ID,desc
                dr["Level"] = "";//=ID,desc
                ResultData.Rows.Add(dr);

                GetTableA(dtInfo, "'" + strArr[0] + "'");

                if (strTag != "")
                {
                    strTag += strArr[0];
                    tagsName = strTag.Split(',');

                    object[] args = new object[1];
                    args[0] = GetString(tagsName);

                    double[] tagsValue = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValues", args);

                    for (int i = 0; i < ResultData.Rows.Count; i++)
                    {
                        string valShow = "";
                        int id = Array.IndexOf((string[])tagsName, ResultData.Rows[i]["id"].ToString()); // 这里的1就是你要查找的值

                        if (id == -1)
                            continue;

                        int _id = tagsName.ToList().FindIndex(p => p == ResultData.Rows[i]["id"].ToString());

                        if (tagsValue[id] != -99999)
                            valShow = tagsValue[id].ToString();
                        else
                            valShow = "0(-99999)";

                        ResultData.Rows[i]["VALUE"] = valShow;
                    }
                }
            }

            return ResultData;// ResultData;
        }
        public static string[] GetString(string[] values)
        {
            List<string> list = new List<string>();
            for (int i = 0; i < values.Length; i++)//遍历数组成员  
            {
                if (list.IndexOf(values[i].ToLower()) == -1)
                    //对每个成员做一次新数组查询如果没有相等的则加到新数组  
                    list.Add(values[i]);
            }
            return list.ToArray();
        }
        private void GetTableA(DataTable Data, string PID)
        {
            DataRow[] drs = Data.Select(string.Format("t_tagid={0}", PID));

            if (drs != null && drs.Length > 0)
            {
                foreach (DataRow dr in drs)
                {
                    string[] Child = dr["t_para"].ToString().Split(',');

                    foreach (string ID in Child)
                    {
                        DataRow drn = ResultData.NewRow();
                        strTag += ID.Replace("'", "") + ",";
                        drn["ID"] = ID.Replace("'", "");
                        drn["PID"] = PID.Replace("'", "");
                        drn["DESC"] = ID.Replace("'", "") + ":" + dr["t_desc"];
                        drn["VALUE"] = null;
                        drn["FORMULA"] = dr["t_FORMULA"];// "";//=ID,desc
                        drn["Level"] = dr["i_Level"];// ID,desc
                        ResultData.Rows.Add(drn);
                        GetTableA(Data, ID);
                    }
                }
            }
        }


        static string tree = "";
        static int num = 0;
        static int count = 0;
        private void GetTree(DataTable dt, string id)
        {
            string parentId = "";
            //try
            //{


            List<string> listID = new List<string>();
            string[] strID = dt.AsEnumerable().Select(T => T["T_PARA"].ToString()).ToArray();
            for (int i = 0; i < strID.Length; i++) listID.Add(strID[i]);


            string[] arrID = id.Split(',');

            for (int n = 0; n < arrID.Length; n++)
            {
                DataRow[] dr = dt.Select("T_TAGID = " + arrID[n]);

                if (dr.Length != 0)
                {
                    parentId += dr[0]["T_PARA"] + ",";
                    string[] cityName = dt.AsEnumerable().Where(T => listID.Contains(T.Field<string>(arrID[n]).ToString())).Select(T => T["T_TAGID"].ToString()).Distinct().ToArray();

                    //string[] name = dt.AsEnumerable().Where(T => T.Field<string>("T_PARA").Contains(arrID[n])).Select(T => T.Field<string>("T_TAGID")).ToArray();

                    //if (name == null)
                    //    tree += "{id:" + dr[0]["T_TAGID"] + ",name:" + dr[0]["T_DESC"] + "(" + dr[0]["T_TAGID"] + "),parnetID:0},";
                    //else
                    //    tree += "{id:" + dr[0]["T_TAGID"] + ",name:" + dr[0]["T_DESC"] + ",parnetID:" + name[0] + "},";
                }
                else
                {
                    tree += "{id:" + arrID[n] + ",name:(" + arrID[n] + "),parnetID:" + arrID[n] + "},";
                }
            }

            if (parentId != "")
            {
                num++;
                parentId = parentId.Substring(0, parentId.Length - 1);
                GetTree(dt, parentId);
            }
            //}
            //catch (Exception ex)
            //{
            //    string s = ex.Message;
            //}
        }

        #endregion

        private void GetTableA(DataTable Data, string PID, DateTime st, int timeCount)
        {
            DataRow[] drs = Data.Select(string.Format("t_tagid={0}", PID));

            if (drs != null && drs.Length > 0)
            {
                foreach (DataRow dr in drs)
                {
                    string[] Child = dr["t_para"].ToString().Split(',');

                    foreach (string ID in Child)
                    {
                        DataRow drn = ResultData.NewRow();
                        strTag += ID.Replace("'", "") + ",";
                        drn["ID"] = ID.Replace("'", "");
                        drn["PID"] = PID.Replace("'", "");
                        drn["DESC"] = ID.Replace("'", "") + ":" + dr["t_desc"];
                        drn["VALUE"] = null;
                        drn["FORMULA"] = dr["t_FORMULA"];// "";//=ID,desc
                        drn["Level"] = dr["i_Level"];// ID,desc

                        for (int i = 0; i <= timeCount; i++)
                        {
                            drn[st.AddMinutes(i * 5).ToString("yyyy-MM-dd H:mm:00")] = null;// ID,desc
                        }

                        ResultData.Rows.Add(drn);
                        GetTableA(Data, ID, st, timeCount);
                    }
                }
            }
        }

        public DataTable GetTableForTPTree(string strInput, DateTime sTime, DateTime eTime)
        {
            ResultData = new DataTable();
            ResultData.Columns.Add("ID");
            ResultData.Columns.Add("PID");
            ResultData.Columns.Add("DESC");//=ID,desc
            ResultData.Columns.Add("VALUE");//=ID,desc
            ResultData.Columns.Add("FORMULA");//=ID,desc
            ResultData.Columns.Add("Level");//=ID,desc

            TimeSpan ts = eTime - sTime;

            int timeCount = (int)ts.TotalMinutes / 5;

            //动态加载列
            for (int i = 0; i <= timeCount; i++)
                ResultData.Columns.Add(sTime.AddMinutes(i * 5).ToString("yyyy-MM-dd H:mm:00"));//=ID,desc


            string sql = string.Format("select * from t_base_calcpara");

            DataTable dtInfo = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { sql, strErrMsg });

            if (dtInfo != null && dtInfo.Rows.Count > 0)
            {
                string[] tagsName = null;
                string[] strArr = strInput.Split(',');

                //添加父节点
                DataRow dr = ResultData.NewRow();

                dr["ID"] = strArr[0];
                dr["PID"] = -1;
                dr["DESC"] = strArr[1];
                dr["VALUE"] = "";
                dr["FORMULA"] = "";//=ID,desc
                dr["Level"] = "";//=ID,desc
                for (int i = 0; i <= timeCount; i++)
                    dr[sTime.AddMinutes(i * 5).ToString("yyyy-MM-dd H:mm:00")] = null;//  ResultData.Columns.Add(sTime.AddMinutes(i * 5).ToString("yyyy-MM-dd H:mm:00"));//=ID,desc

                ResultData.Rows.Add(dr);

                //GetTableA(dtInfo, "'" + strArr[0] + "'");
                GetTableA(dtInfo, "'" + strArr[0] + "'", sTime, timeCount);

                if (strTag != "")
                {
                    strTag += strArr[0];
                    tagsName = strTag.Split(',');

                    sql = string.Format("SELECT * FROM t_info_HistData where t_time >='{0}' and t_time <='{1}'", sTime.ToString("yyyy-MM-dd H:mm:ss"), sTime.AddMinutes(timeCount * 5).ToString("yyyy-MM-dd H:mm:ss"));

                    DataTable dtHistData = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { sql, strErrMsg });

                    if (dtHistData != null && dtHistData.Rows.Count > 0)
                    {
                        for (int k = 0; k < ResultData.Rows.Count; k++)
                        {
                            for (int i = 0; i <= timeCount; i++)
                            {
                                DataRow[] drs = dtHistData.Select("t_tag='" + ResultData.Rows[k]["ID"] + "' and t_time = '" + sTime.AddMinutes(i * 5).ToString("yyyy-MM-dd H:mm:00") + "'");

                                if (drs != null && drs.Length > 0 && drs[0]["d_value"].ToString() != "")
                                    ResultData.Rows[k][sTime.AddMinutes(i * 5).ToString("yyyy-MM-dd H:mm:00")] = drs[0]["d_value"];
                                else
                                    ResultData.Rows[k][sTime.AddMinutes(i * 5).ToString("yyyy-MM-dd H:mm:00")] = "查询不到";
                            }
                        }
                    }
                }
            }
            return ResultData;// ResultData;
        }

        #region 获取所有测点信息
        public IList<Hashtable> GetStationPoints(string parentId, string pointDes)
        {
            IList<Hashtable> list = new List<Hashtable>();

            //获取所有电站信息
            strSql = "select T_ORGID from T_INFO_PLANT";
            DataTable dtStations = new DataTable();
            dtStations = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });

            //获取子集
            strSql = "select T_ORGID from T_BASE_ORG where T_PARENTID ='" + parentId + "'";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            GetList(dt, dtStations);

            DataTable dtArea = new DataTable();
            if (stationId != "")
            {
                stationId = stationId.Substring(0, stationId.Length - 1);
                strSql = "select T_DISTRICTID,T_DISTRICTDESC from T_BASE_DISTRICT where T_ORGID in(" + stationId + ")";
            }
            else
                strSql = "select T_DISTRICTID,T_DISTRICTDESC from T_BASE_DISTRICT where T_ORGID='" + parentId + "'";
            if (pointDes != "")
                strSql += " and T_DISTRICTDESC like '%" + pointDes + "%'";
            dtArea = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });


            list = DataTableToList(dtArea);
            return list;
        }
        #endregion


        #region 获取电站理论发电量
        public double[] GetTheoreticalQuantity(string id)
        {
            double[] val = new double[12];
            strSql = "select D_TENERGY1,D_TENERGY2,D_TENERGY3,D_TENERGY4,D_TENERGY5,D_TENERGY6,D_TENERGY7,D_TENERGY8,D_TENERGY9,D_TENERGY10,D_TENERGY11,D_TENERGY12 from T_INFO_PLANT where T_ORGID = '" + id + "'";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });

            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    val[i] = Convert.ToDouble(dt.Rows[0][i].ToString());
                }
            }
            return val;
        }
        #endregion

        #region 获取PR值
        public double[][] GetStationPR(string id, string names, DateTime sTime, DateTime eTime, int second)
        {
            double[][] val = null;
            strSql = "select T_PRTAG from T_BASE_POINTS_ORG where T_ORGID in(" + id + ")";

            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
            string[] points = new string[dt.Rows.Count];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                points[i] = dt.Rows[i][0].ToString();
            }

            val = (double[][])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValuesByStep", new object[] { points, sTime, eTime, second });

            return val;
        }
        #endregion
    }
}